473,382 Members | 1,313 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,382 software developers and data experts.

weak random ?

Hello
i call it in almost same time (from two different threads):
Random rand = new Random();
id=rand.Next(65535);

and i often receive the same results.
Is it possible ? How windows count this random ?
Thanx

Nov 15 '05 #1
5 2193
Well from the docs:

Random rand = new Random();
Initializes a new instance of the Random class, using a time-dependent
default seed value.

If two threads are running simultaneously and executes this call with only
microseconds in time difference, you will definately get the same random
sequence.

Make sure there is a delay between the instantiation of the calls for the
two threads and you should be fine.
Arild
<us**@domain.invalid> wrote in message
news:uG****************@TK2MSFTNGP09.phx.gbl...
Hello
i call it in almost same time (from two different threads):
Random rand = new Random();
id=rand.Next(65535);

and i often receive the same results.
Is it possible ? How windows count this random ?
Thanx

Nov 15 '05 #2
The Random class doesn't really produce random numbers. It produces
seemingly random numbers using a formula and a starting number. If you
use the same starting number, you will get the same sequence of numbers.
The get better "randomness" I prefer to use this way of initializing
Random by using DateTime.Now's Tick property.

Random rand = new Random((int)DateTime.Now.Ticks);

Happy coding!
Morten
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3
Even given that, we noticed a distinct pattern in random when we were using
it, and ended up calling it 6 or 7 times just to kick start it into some
degree of unpredictability. If you actually need a proper random number
generator (for encryption etc, where it could matter) you might check out
the Boost libraries - they have some good stuff.

Steve

"Arild Bakken" <ar*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Well from the docs:

Random rand = new Random();
Initializes a new instance of the Random class, using a time-dependent
default seed value.

If two threads are running simultaneously and executes this call with only
microseconds in time difference, you will definately get the same random
sequence.

Make sure there is a delay between the instantiation of the calls for the
two threads and you should be fine.
Arild
<us**@domain.invalid> wrote in message
news:uG****************@TK2MSFTNGP09.phx.gbl...
Hello
i call it in almost same time (from two different threads):
Random rand = new Random();
id=rand.Next(65535);

and i often receive the same results.
Is it possible ? How windows count this random ?
Thanx


Nov 15 '05 #4
If you are looking for a stronger Random generator, you should look at:

System.Security.Cryptography.RNGCryptoServiceProvi der

José
<us**@domain.invalid> wrote in message
news:uG****************@TK2MSFTNGP09.phx.gbl...
Hello
i call it in almost same time (from two different threads):
Random rand = new Random();
id=rand.Next(65535);

and i often receive the same results.
Is it possible ? How windows count this random ?
Thanx

Nov 15 '05 #5
us**@domain.invalid wrote:

Hello
i call it in almost same time (from two different threads):
Random rand = new Random();
id=rand.Next(65535);

and i often receive the same results.
Is it possible ? How windows count this random ?

Thanx


Random is *not* a random number generator, but rather a *pseudo* random number
generator. Given the same seed, it will always produce the same number
sequence.

From the help:
The random number generation starts from a seed value. If the same seed is used
repeatedly, the same series of numbers is generated. One way to produce
different sequences is to make the seed value time-dependent, thereby producing
a different series with each new instance of Random.

The trick is to use a random seed. In your case, a good candidate for the seed
might be the thread id.
Nov 15 '05 #6

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

Similar topics

94
by: Gabriel Zachmann | last post by:
Is it correct to say that strong/weak typing does not make a difference if one does not use any pointers (or adress-taking operator)? More concretely, I am thinking particularly of Python vs C++....
4
by: Vinay | last post by:
Hello My question is regarding "weak external symbols". Consider the following eg. class test { public : int func1(void); {cout <<"func1";} int func2(void);
3
by: memememe | last post by:
I see weak reference on the .net api, but I do not see soft or phantom, are they supported on .net?
7
by: Derrick | last post by:
I'm loading a boatload of data into a DataSet. The memory usage grows and grows for the app while loading that data. Calling GC.Collect() reduces the consumption slightly. When I minimize the...
2
by: Matthew Herrmann | last post by:
Hi, I've heard from groups that listeners to event handlers cause references to be kept alive, if the targets are marked to stay alive. I need to make sure that attaching events to objects will...
9
by: Neelesh Bodas | last post by:
Hi all, does C++ give a way to declare a specific symbol as a "weak symbol"? In 'C', one can use #pragma directive. Is this still a legal way in C++ ? Is there a (better?) alternative?
3
by: John Nagle | last post by:
Are weak refs slower than strong refs? I've been considering making the "parent" links in BeautifulSoup into weak refs, so the trees will release immediately when they're no longer needed. In...
1
by: Henri.Chinasque | last post by:
Hi all, I've been considering that my objects should subscribe to an event via a weak reference, however I've found several warnings that this approach comes with concurrency considerations,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.