473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can't get true random numbers

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.
May 23 '06 #1
6 3970

"D. Patrick" <re*****************@thenotreal.com> wrote in message
news:4c*****************@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?

May 23 '06 #2
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.

May 23 '06 #3
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
"D. Patrick" <re*****************@thenotreal.com> wrote in message
news:4c*****************@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.
|
|
May 24 '06 #4
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" <re*****************@thenotreal.com> escribió en el mensaje news:4c*****************@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.

May 24 '06 #5
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:Oy**************@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" <re*****************@thenotreal.com> escribió en el mensaje
news:4c*****************@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.
May 25 '06 #6
Have you considered using the new System.Security.Cryptography.RandomNumberGenerator .
See http://msdn2.microsoft.com/en-us/lib...er(d=ide).aspx
for a sample implementation.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
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:Oy**************@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" <re*****************@thenotreal.com> escribió en el
mensaje
news:4c*****************@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.

May 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
0
by: Tony Stephens | last post by:
I have a database table that contains a set of numbers (set X). I need to be able to generate/export a random set of survey numbers (set C) from set A with IDs that aren't in set B. Where set A is...
10
by: sam | last post by:
hi, i am new to C/C++ and have just finished a program based on the UK national lottery, where you enter 6 numbers, six are generated by the computer and there are appropriate messages and a...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
11
by: TreatmentPlant | last post by:
I need to generate a few thousand true random numbers using C++. I have some code now that does alright, but when you plot the results on a graph, you notice patterns, so the numbers are not...
7
by: bipi | last post by:
Dear all, I found function rand(), it can create random number but this function can not define the range of number which I want to get it, such as, I want to get random number in the range from...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.