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! 2 3660
HumanJHawkins <JH******@Locut ius.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
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_sectio n 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 shelled out on VC++ 6.0 compiling that
same code is proving difficult. I am not too worried how I generate random
numbers in c++, as long as it is sufficiently random.
Can anybody help me out in getting what I want from VC++?
|
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 commit this foul atrocity
against the finer design principles of RNGs in general...
So here's me thought: Design a seeding function that has only one purpose --
and that purpose is to use time(NULL) as it's input, and generate a
reasonably random...
|
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 have utilised the following random number source code
http://www.agner.org/random/
What I have found is that there is a problem with seeding. The code
generates a seed based on time(0). I have found that I need to increment
|
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;
|
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 loop, which does not do the
job. I get the same output from my simulator every time. Does it mean
that it actually can seed ONCE per program run?
Thanks
| |
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 System.Threading;
using System.Web.Services;
|
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 better way than to fill an array of range 0... RAND_MAX with
pre-computed primes and using the output of rand() to index into it to
extract a random prime.
|
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, with it's massive period
of 2^19937-1. Will you only ever have access to a tiny
portion of this ring of numbers, if you only use a 32-bit seed?
Will this set of sequences be confined to the beginning of the
period, ie, your sequence will have to...
|
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 to 100 .
Please help me,
Many thanks,
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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...
|
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...
|
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |