473,799 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

weak random ?

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

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

Nov 15 '05 #1
5 2217
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.in valid> wrote in message
news:uG******** ********@TK2MSF TNGP09.phx.gbl. ..
Hello
i call it in almost same time (from two different threads):
Random rand = new Random();
id=rand.Next(65 535);

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)Dat eTime.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 unpredictabilit y. 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*****@hotmai l.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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.in valid> wrote in message
news:uG******** ********@TK2MSF TNGP09.phx.gbl. ..
Hello
i call it in almost same time (from two different threads):
Random rand = new Random();
id=rand.Next(65 535);

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.R NGCryptoService Provider

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

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

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

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

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
4748
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++. So, are there any examples (without pointers, references, or adress-taking), which would have a different result in Python and in C++? I would appreciate all insights or pointers to literature. TIA,
4
3729
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
2359
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
2471
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 app though, the usage goes to about 500k, and then grows when maximizing the app and working with DataSet. The DataSet still appears to have all data when mem footprint was many many megs at the end of loading it. Two questions: 1) I'm guessing...
2
1550
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 not cause them to be kept open. I created a test which has "target" listening to "source" for events. After plugging source into target, I then let go of source. Since I'm still holding onto target, if delegates were a strong reference, then
9
8237
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
3427
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 general, all links back towards the root of a tree should be weak refs; this breaks the loops that give reference counting trouble. John Nagle
1
1769
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, like the fact that the event handler method on the subscriber could be called and be executing while the object is being garbage collected. Not being super strong in the multithreaded department, I am having a hard time coming up with other...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10269
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.