Random number

M

mm

Hello, I have used these two codes to generate random
numbers.
one=int(rnd()*100000 and
two=CLng(100000*rnd(0))

I have the a form that contains a text box where each of
the numbers is displayed.
These generate five digit numbers just fine, however,
every time I close the database and open the next day I
get the same random numbers in the same order as the day
before. I need to use these random numbers to input
unique ID values for new database entries. Is there a way
that I can generate the random numbers without any
repitition?

Thanks!
 
D

Dirk Goldgar

mm said:
Hello, I have used these two codes to generate random
numbers.
one=int(rnd()*100000 and
two=CLng(100000*rnd(0))

I have the a form that contains a text box where each of
the numbers is displayed.
These generate five digit numbers just fine, however,
every time I close the database and open the next day I
get the same random numbers in the same order as the day
before. I need to use these random numbers to input
unique ID values for new database entries. Is there a way
that I can generate the random numbers without any
repitition?

Thanks!

Execute the Randomize statement before your first call to the Rnd()
function.
 
P

Pavel Romashkin

I think you will not have a guarantee that you will never get the same
number, though. Since you are using them for unique ids, you might want
to use Autonumber instead, which guarantees your number will never
repeat, or at least check existing records for the newly generated
number before inserting it into the table.

Pavel
 
M

mm

Hello, thank you for your response. I am not sure what you
mean by "execute the randomize statement before you call
to the Rnd() function", could you please clear that up for
me.

Thanks!
 
D

Dirk Goldgar

mm said:
Hello, thank you for your response. I am not sure what you
mean by "execute the randomize statement before you call
to the Rnd() function", could you please clear that up for
me.

Thanks!

While in the VB editor, bring up the online help for the Randomize
statement. It "seeds" the random-number generator so that subsequent
calls to the Rnd() function don't bring up the same sequence of numbers
every time. You need to execute the statement only once in any session,
so you could do so in the Open event of your form, or in startup code
for your database.
 

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