Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you create a random integer in range...

Status
Not open for further replies.

ndogg

Programmer
Feb 7, 2000
156
US
I have been trying to find a way of creating a random integer that can go from 0 to x inclusive of all numbers between the two number. Doing numbers from 1 - 10 and 1 - 100 (or any of its multiples) is easy enough, but I need to get a range from 1 - 16, no more, no less. If I can make x be any number, this would be helpful in the future.<br>
Here is the code I am trying to implement it in.<br>
<br>
function random() {<br>
links = new Array(&quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> for (var i,a = Math.random(),b = 10*a,c = Math.round(b); i = b && i &lt;= links.length; i++) {<br>
window.location.href=links<i><br>
}<br>
}<br>
<br>
 
I just wanted everyone to know that the code is not correct, I just needed for people to get an idea of what I wanted it to be used in. The idea I was trying to get was to create a function that when called would take the person to a random link and the function could really be changed to do what ever the person wanted.<br>
The real code is below (sorry about any confusion).<br>
<br>
function random() {<br>
links = new Array(&quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> &quot;<A HREF=" TARGET="_new"> var a = Math.random()<br>
var b = links.length*a<br>
var c = Math.round(b)<br>
window.location.href=links[c]<br>
}
 
<b>function CheckTooSmall(InputName){<br>
&nbsp;&nbsp;&nbsp;&nbsp;if InputName &lt; y { //Checks to see if variable is below lower (y) variable<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name ++; //adds 1 to the number<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CheckTooSmall(InputName);<br>
&nbsp;&nbsp;&nbsp;&nbsp;}else{return true};<br>
}; //Goes back to place where function was called<br>
<br>
var someNumber = math.random*x; //Creates random number less than x<br>
CheckTooSmall(someNumber); //Calls routine created earlier</b><br>
<br>
<br>
This will create a random number greated than or equal to y, but less than x :)<br>
<p>-Robherc<br><a href=mailto:robherc@netzero.net>robherc@netzero.net</a><br><a href= > </a><br>*nix installation & program collector/reseller. Contact me if you think you've got one that I don't :)
 
I have had some luck with<br>
Math.round(Math.random() + lower_end_number * higher_end_number;)<br>
<br>
or something like that.<br>
<br>
Play around with it, see what it does, but just a warning, I am pulling that out of the far far reaches of my brain, seeing as how I havn't used it in a while.<br>
<br>
theEclipse<br>
robacarp.webjump.com
 
Math.random() generates a pseudorandom number between 0 and 1. The code below stretches the interval (0,1) to (0,maximum-minimum), then adds minimum to get the new interval (minimum, maximum), then uses Math.floor to get an integer in the interval. Use whatever you like for minimum and maximum.<br>
<br>
&lt;script language=&quot;JavaScript&quot;&gt;<br>
var minimum=3<br>
var maximum=18<br>
if (maximum&lt;minimum){<br>
minimum = maximum<br>
}<br>
maximum = maximum+1 // so user's maximum is selectable<br>
randomnumber = Math.floor(minimum + (maximum-minimum)*Math.random())<br>
document.write('Generated pseudo-random number is ' + randomnumber)<br>
&lt;/script&gt;<br>
<br>
<br>

 
Actually, I was talking to my Calculus teacher and he told me to just multiply the resulting number times the highest number in the range I wanted (since the number is from zero to one). Then I needed to have the resulting number rounded. Getting a zero doesn't happen to often, but I can always do some function that checks for a zero nonetheless. Something like this:<br>
<br>
function random(i) // i is the highest number in the range<br>
{<br>
a = Math.random()<br>
b = i*a<br>
c = Math.round(b)<br>
if (c = 0) { random(i) }<br>
else<br>
return i<br>
}<br>
<br>
It's simple and simplicity is always nice. <p>REH<br><a href=mailto:hawkdogg@crosswinds.net>hawkdogg@crosswinds.net</a><br><a href= by Linux</a><br>Learn Linux and Leave out the Windows :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top