Sorry meant Rnd number duplication

R

RioDeSmoke

I have some code that loops through a Recordset in order to select an X
amount of random records. When I select 10 records I get 10 -- 20 I get 20
and so on. But, when I select 400, I get 369 at times and 381 at others.
The difference gets greater the more records I select. I believe what is
happening is that the random numbers being generated are being duplicated and
it can't select the record more than once. Here is my code within my loop:

MyRS.MoveLast
NumOfRecords = MyRS.RecordCount
Randomize
SpecificRecord = Int((NumOfRecords * Rnd) + 1)

Has anyone ever seen this? Please help if you can this is driving me crazy!
 
J

John Vinson

I have some code that loops through a Recordset in order to select an X
amount of random records. When I select 10 records I get 10 -- 20 I get 20
and so on. But, when I select 400, I get 369 at times and 381 at others.
The difference gets greater the more records I select. I believe what is
happening is that the random numbers being generated are being duplicated and
it can't select the record more than once. Here is my code within my loop:

MyRS.MoveLast
NumOfRecords = MyRS.RecordCount
Randomize
SpecificRecord = Int((NumOfRecords * Rnd) + 1)

Has anyone ever seen this? Please help if you can this is driving me crazy!

Well, if you pick truly random integers in the range 1 to 400, then
absolutely, you WILL get duplicates. If you put 400 numbered beads in
a bag, and draw twenty with replacement, there's a very good chance
that you'll draw the same one twice; if you draw 100, it's almost a
certainty.

If you want to select 400 randomly chosen records from a recordset,
add a calculated random number field; *sort* ascending (or descending,
makes no difference) by it to shuffle the records into random order;
and use the Top Values property of the query to select the top 400
records.

John W. Vinson[MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top