|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Can't get true random numbersI 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. "D. Patrick" <replywithinthegr***@thenotreal.com> wrote in message How many numbers total do you need? Why not generate them all when the 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. program loads and use them as needed after that? 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. 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. -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "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. | | 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. 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,news:OyFwbhzfGHA.4892@TK2MSFTNGP02.phx.gbl... 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. 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.
What's the most performant? Fat our smart client?
CHRW .Net Mail and hotmail question MDI Form mdiList for experts Login Dialog What are they? output data from the result of storedprocedure into excel format Controls do not show correctly at runtime. 2 small questions RE: Communicate between 2 applications on the same machine |
|||||||||||||||||||||||