473,396 Members | 2,106 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.

Seeding Random Numbers in Multiple Threads?

I wrote this question a little differently in the MFC group since it is
a bit of a different environment than pure C++, but I hope you will
forgive the similarities if anyone is reading both groups.

Basically, I need to seed the random number generator in 4 seperate
threads at once. So, I can't use the current time as the random seed,
because then all four threads end up with the same series of (therefore
non-random) numbers.

Is there a best practice for seeding multiple random number generators
at once?

Thanks in advance!

Nov 30 '06 #1
2 3595
HumanJHawkins <JH******@Locutius.Comwrote:
>I wrote this question a little differently in the MFC group since it is
a bit of a different environment than pure C++, but I hope you will
forgive the similarities if anyone is reading both groups.

Basically, I need to seed the random number generator in 4 seperate
threads at once. So, I can't use the current time as the random seed,
because then all four threads end up with the same series of (therefore
non-random) numbers.

Is there a best practice for seeding multiple random number generators
at once?
Obtain "seed" from the current time, then use

seed
seed + 1000
seed + 2000
seed + 3000

Steve
Nov 30 '06 #2

HumanJHawkins skrev:
I wrote this question a little differently in the MFC group since it is
a bit of a different environment than pure C++, but I hope you will
forgive the similarities if anyone is reading both groups.

Basically, I need to seed the random number generator in 4 seperate
threads at once. So, I can't use the current time as the random seed,
because then all four threads end up with the same series of (therefore
non-random) numbers.
If you are talking about the standard random generator and srand, you
better check the implementation. Most probably it is not thread-safe.
In any case, by protecting rand() with a mutex (of some type - a
windows critical_section looks best), there is no reason to seed more
than once.
>
Is there a best practice for seeding multiple random number generators
at once?
If you use e.g. boost and let each thread have its own generator you
could e.g. use time and add the threadid to that value.

/Peter

Nov 30 '06 #3

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

Similar topics

16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
2
by: AngleWyrm | last post by:
Ok, I know that srand( time(NULL) ) isn't such a good idea, but I do it anyway. Why? The clock is an easy source of randomness. And I've got this sneaky suspicion that I'm not the first guy to...
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...
4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
3
by: bobrics | last post by:
Hi, I am using srand() and would like to create different random numbers during a SINGLE execution of my program because I want to compare random cases. For now I have a switch statement within a...
11
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
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...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
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...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...
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
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,...
0
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...

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.