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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating Random Numbers...

Status
Not open for further replies.

famousb

Programmer
Mar 2, 2000
239
0
0
US
ok, i've done this before but can't remember how.<br>
I need to generate 100 unique random numbers ranging from 1-X, with X being a value enterred by the user.<br>
I thought i did this before through a query, but i can't seem to get it to work now. the formula i'm using now is<br>
<br>
Int([Upperbound]*Rnd())<br>
<br>
the problem with this is that it returns the same number 100 times, as oppossed to a different one. i know it's possible, i've done it before.<br>
<br>
thanks <p>Brian Famous<br><a href=mailto:bfamous@ncdoi.net>bfamous@ncdoi.net</a><br><a href= > </a><br>
 
Well if you want 100 differnt numbers with NO one EVER being the same then you have to keep track of them some how.<br>
Right. In an array or something...<br>
<br>
I guess 1-X means a number from 1 to X and not 1 Minus X<br>
<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
yes, 1 to X, not 1 minus X. - sorry, too much green beer this weekend.<br>
<br>
and the recordsets will be around 5 million numbers, so i'm not too concerned about the possibility of generating the same number twice. i can always fix that on the back end.<br>
<br>
<p>Brian Famous<br><a href=mailto:bfamous@ncdoi.net>bfamous@ncdoi.net</a><br><a href= > </a><br>
 
Well I have noticed the same problem.<br>
Random is not very random<br>
What you have to do is keep track of each number it generates and force it to re-generate a new number until it comes up with a differnent one.<br>
This may take a while.<br>
<br>
<p> DougP<br><a href=mailto: dposton@universal1.com> dposton@universal1.com</a><br><a href= > </a><br>
 
well, this is the formula i came up with<br>
<br>
Random: Int(([UpperLimit])*Rnd([UpperLimit]-[PlaceHolder]))<br>
<br>
i put this in the Field line of the query, and then have a field named [PlaceHolder] that is linked to a table with the numbers 1 through 100 (100 being the largest sample i will need.)<br>
<br>
it prompts for the [UpperLimit] which is the number of records that i need to sample at the particular time. it seems to work pretty good so far so it will do, but it's not what i did before. i'm just not really going to worry about the possibility of generating the same number twice, i'll cross that bridge when i come to it. <p>Brian Famous<br><a href=mailto:bfamous@ncdoi.net>bfamous@ncdoi.net</a><br><a href= > </a><br>
 
If you used your numbers as the primary key in a new table, dupes would be thrown out for you.
 
Use the Randomize statement. See below.<br>
<br>
Randomize Statement<br>
<br>
Initializes the random-number generator.<br>
<br>
Syntax:<br>
<br>
Randomize [number]<br>
<br>
The optional numberargument is aVariant or any validnumeric expression.<br>
<br>
Remarks:<br>
<br>
Randomize uses number to initialize the Rnd function's random-number generator, giving it a newseed value. If you omit number, the value returned by the system timer is used as the new seed value.<br>
<br>
If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.<br>
<br>
Note: To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.<br>
<br>
<br>
Randomize Statement Example:<br>
<br>
This example uses the Randomize statement to initialize the random-number generator. Because the number argument has been omitted, Randomize uses the return value from the Timer function as the new seed value.<br>
<br>
Dim MyValue<br>
Randomize ' Initialize random-number generator.<br>
<br>
MyValue = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6.<br>
<br>
Mike<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top