Can't get true random numbers

D

D. Patrick

I have a multi-threaded app that needs to generate random 8 digit numbers.
I'm using VB.NET 2.0. The problem is that >1 request for a random number
can occur in the same millisecond. I can't seed with a time value when the
time for 2 (or more) threads is identical. Even ticks is identical. So
how can I generate a random number that will not likely be the same as the
number returned by another thread running at that same millisecond? It
seems computers are getting too fast these days to just seed with the time.
 
H

Homer J Simpson

I have a multi-threaded app that needs to generate random 8 digit numbers.
I'm using VB.NET 2.0. The problem is that >1 request for a random number
can occur in the same millisecond. I can't seed with a time value when
the time for 2 (or more) threads is identical. Even ticks is identical.
So how can I generate a random number that will not likely be the same as
the number returned by another thread running at that same millisecond?
It seems computers are getting too fast these days to just seed with the
time.

How many numbers total do you need? Why not generate them all when the
program loads and use them as needed after that?
 
C

Chris Dunaway

Use Environment.GetTickCount() as the seed for a single instance of the
Random class which you pass into each thread. As each thread calls it,
protect it with a SyncLock statement so that each thread will get
unique values.
 
J

Jay B. Harlow [MVP - Outlook]

D. Patrick,
Have you considered rather then allocate a new Random for each thread.

Create a single Random that is shared by *all* threads, I would create this
single Random before creating any of the threads.


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|I have a multi-threaded app that needs to generate random 8 digit numbers.
| I'm using VB.NET 2.0. The problem is that >1 request for a random number
| can occur in the same millisecond. I can't seed with a time value when
the
| time for 2 (or more) threads is identical. Even ticks is identical. So
| how can I generate a random number that will not likely be the same as the
| number returned by another thread running at that same millisecond? It
| seems computers are getting too fast these days to just seed with the
time.
|
|
 
?

=?Windows-1252?Q?Jos=E9_Manuel_Ag=FCero?=

Hello D. Patrick,

The best approach using the Random class might be to create a class that holds a single Random instance and offers a shared, thread safe, property that returns a random number. Your threads would get the random number through this property.

Regards.


"D. Patrick" <[email protected]> escribió en el mensaje |I have a multi-threaded app that needs to generate random 8 digit numbers.
| I'm using VB.NET 2.0. The problem is that >1 request for a random number
| can occur in the same millisecond. I can't seed with a time value when the
| time for 2 (or more) threads is identical. Even ticks is identical. So
| how can I generate a random number that will not likely be the same as the
| number returned by another thread running at that same millisecond? It
| seems computers are getting too fast these days to just seed with the time.
 
J

Jay B. Harlow [MVP - Outlook]

José,
Doh! yes wrapping Random in a thread safe class would be a good idea!


--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


"José Manuel Agüero" <chema012_hotmail.com> wrote in message
Hello D. Patrick,

The best approach using the Random class might be to create a class that
holds a single Random instance and offers a shared, thread safe, property
that returns a random number. Your threads would get the random number
through this property.

Regards.


"D. Patrick" <[email protected]> escribió en el mensaje
|I have a multi-threaded app that needs to generate random 8 digit numbers.
| I'm using VB.NET 2.0. The problem is that >1 request for a random number
| can occur in the same millisecond. I can't seed with a time value when
the
| time for 2 (or more) threads is identical. Even ticks is identical. So
| how can I generate a random number that will not likely be the same as the
| number returned by another thread running at that same millisecond? It
| seems computers are getting too fast these days to just seed with the
time.
 

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