Home All Groups Group Topic Archive Search About

Can't get true random numbers

Author
23 May 2006 6:08 PM
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.

Author
23 May 2006 7:18 PM
Homer J Simpson
"D. Patrick" <replywithinthegr***@thenotreal.com> wrote in message
news:4cIcg.3946$9W5.1121@tornado.socal.rr.com...

> 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?
Author
23 May 2006 8:47 PM
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.
Author
24 May 2006 12:39 PM
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


Show quoteHide quote
"D. Patrick" <replywithinthegr***@thenotreal.com> wrote in message
news:4cIcg.3946$9W5.1121@tornado.socal.rr.com...
|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.
|
|
Author
24 May 2006 1:45 PM
José_Manuel_Agüero
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.


Show quoteHide quote
"D. Patrick" <replywithinthegr***@thenotreal.com> escribió en el mensaje news:4cIcg.3946$9W5.1121@tornado.socal.rr.com...
|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.
Author
25 May 2006 12:15 AM
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
news:OyFwbhzfGHA.4892@TK2MSFTNGP02.phx.gbl...
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.


Show quoteHide quote
"D. Patrick" <replywithinthegr***@thenotreal.com> escribió en el mensaje
news:4cIcg.3946$9W5.1121@tornado.socal.rr.com...
|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.
Author
25 May 2006 2:13 PM
Jim Wooley
Have you considered using the new System.Security.Cryptography.RandomNumberGenerator.
See http://msdn2.microsoft.com/en-us/library/system.security.cryptography.rngcryptoserviceprovider(d=ide).aspx
for a sample implementation.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx

Show quoteHide quote
> José,
> Doh! yes wrapping Random in a thread safe class would be a good idea!
> "José Manuel Agüero" <chema012_hotmail.com> wrote in message
> news:OyFwbhzfGHA.4892@TK2MSFTNGP02.phx.gbl...
> 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" <replywithinthegr***@thenotreal.com> escribió en el
> mensaje
> news:4cIcg.3946$9W5.1121@tornado.socal.rr.com...
> |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.