how to generate uncorrelated random sequences using c++ - c++

I'd like to generate two sequences of uncorrelated normal distributed random numbers X1, X2.
As normal distributed random numbers come from uniform numbers, all I need is two uncorrelated uniform sequences. But how to do it using:
srand (time(NULL));
I guess I need to seed twice or do something similar?

Since the random numbers generated by a high-quality random-number generator are uniform and independent, you can generate as many independent sequences from it as you like.
You do not need, and should not seed two different generators.
In C++(11), you should use a pseudo-random number generator from the header <random>. Here’s a minimal example that can serve as a template for an actual implementation:
std::random_device seed;
std::mt19937 gen{seed()};
std::normal_distribution<> dist1{mean1, sd1};
std::normal_distribution<> dist2{mean2, sd2};
Now you can generate independent sequences of numbers by calling dist1(gen) and dist2(gen). The random_device is used to seed the actual generator, which in my code is a Mersenne Twister generator. This type of generator is efficient and has good statistical properties. It should be considered the default choice for a (non cryptographically secure) generator.

rand doesn't support generating more than a single sequence. It stores its state in a global variable. On some systems (namely POSIX-compliant ones) you can use rand_r to stay close to that approach. You'd simply use some initial seed as internal state for each. But since your question is tagged C++, I suggest you use the random number facilities introduced in C++11. Or, if C++11 is not an option, use the random module from boost.
A while ago I've asked a similar question, Random numbers for multiple threads, the answers to which might be useful for you as well. They discuss various aspects of how to ensure that sequences are not interrelated, or at least not in an obvious way.

Use two random_devices (possibly with some use of engine) with a normal_distribution from <random> :
std::random_device rd1, rd2;
std::normal_distribution d;
double v1 = d(rd1);
double v2 = d(rd2);
...
See also example code at http://en.cppreference.com/w/cpp/numeric/random/normal_distribution

Related

Match pseudo random numbers between MT19937 CPU and GPU

I am studying the behaviour of CURAND_RNG_PSEUDO_MT19937, specifically in order to match numbers generated by the standard CPU implemetation of Mersenne Twister (std::mt19937 or boost::random::mt19937).
I read in the documentation that cuRand MT19937 “has the same parameters as CPU version, but ordering is different […] Output is generated by 8192 independent generators. Each generator generates consecutive subsequence of the original sequence. […] Results are permuted differently than originally to achieve higher performance.”
Checking the unsigned int output sequences of std::mt19937 and cuRand MT19937 with the same seed, only the first number is equal and immediately the two generators diverge.
Consider that in my CPU environment, I have a distributed computation that instantiate n std::mt19937 with incremental seeds (s+1, s+2, etc.). Do you know if there is a way to modify the cuRand MT19937 generators in order to match my workflow?
Thanks

Do std::random_device and std::mt19937 follow an uniform distribution?

I'm trying to convert this line of matlab in C++: rp = randperm(p);
Following the randperm documentation:
randperm uses the same random number generator as rand
And in rand page:
rand returns a single uniformly distributed random number
So rand follows an uniform distribution. My C++ code is based on:
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(... , ... ,g);
My question is: the code above follows an uniform distribution? If not, how to do so?
The different classes from the C++ random number library roughly work as follows:
std::random_device is a uniformly-distributed random number generator that may access a hardware device in your system, or something like /dev/random on Linux. It is usually just used to seed a pseudo-random generator, since the underlying device wil usually run out of entropy quickly.
std::mt19937 is a fast pseudo-random number generator using the Mersenne Twister engine which, according to the original authors' paper title, is also uniform. This generates fully random 32-bit or 64-bit unsigned integers. Since std::random_device is only used to seed this generator, it does not have to be uniform itself (e.g., you often seed the generator using a current time stamp, which is definitely not uniformly distributed).
Typically, you use a generator such as std::mt19937 to feed a particular distribution, e.g. a std::uniform_int_distribution or std::normal_distribution which then take the desired distribution shape.
std::shuffle, according to the documentation,
Reorders the elements in the given range [first, last) such that each possible permutation of those elements has equal probability of appearance.
In your code example, you use the std::mt19937 PRNG to feed std::shuffle. So, std::mt19937 is uniform, and std::shuffle should also behave uniformly. So, everything is as uniform as it can be.

How to properly initialize a C++11 std::seed_seq

I have a C++11 program that needs to create several independent random generators, for use by different threads in a parallel computation. These generators should be initialized with different seed values so that they all produce different pseudo-random sequences.
I see that there's a std::seed_seq class that seems to be meant for this purpose, but it's not clear what's the right way to construct one. The examples I've seen, such as the one on cppreference.com, initialize it with a handful of integer constants hard-coded in the program:
std::seed_seq seq{1,2,3,4,5};
I doubt that's actually a recommended best practice, so I'm wondering what is the recommended practice. In particular:
Since a seed_seq can be initialized with an arbitrary number of integers, what's the significance of the length of its initializer list? If I want to produce seeds for 100 random generators, do I need to initialize my seed_seq with 100 integers?
If the length of the initializer list doesn't have to match the number of seeds I intend to generate, is it OK to initialize a seed_seq with just one integer and then use it to produce a large number of seeds?
How about initializing with no integers, i.e. using the default constructor? (This means I'd get the same seeds every time, of course.)
If it's OK to construct a seed_seq from a single integer and then generate lots of seeds from it, what's the benefit of using seed_seq instead of an ordinary random generator? Why not just construct a std::mt19937 from that single integer and use that to produce seed values for other generators?
The trouble with using a fixed sequence like that is that you get the same sequence of seeds out of it, much the same as if you had called srand(42) at the start of your program: it generates identical sequences.
The C++11 standard states (in section 26.5.7.1 Class seed_seq):
A seed sequence is an object that consumes a sequence of integer-valued data and produces a requested number of unsigned integer values i, 0 i < 232, based on the consumed data.
[Note: Such an object provides a mechanism to avoid replication of streams of random variates. This can be useful, for example, in applications requiring large numbers of random number engines. —end note]
It also states how those integers are turned into seeds in paragraph 8 of that section, in such a way that the distribution of those seeds is acceptable even if the integer input items are very similar. So you can probably think of it as a pseudo-random number generator for seed values.
A larger number of items will provide more "randomness" in the seed values, provided they have some randomness themselves. Using constants as input is a bad idea for this reason.
What I tend to do is very similar to the way you normally randomise one generator, with srand (time (0)). In other words:
#include <random>
#include <cstdint>
#include <ctime>
#include <iostream>
int main()
{
std::seed_seq seq{time(0)};
std::vector<std::uint32_t> seeds(10);
seq.generate(seeds.begin(), seeds.end());
for (std::uint32_t n : seeds) {
std::cout << n << '\n';
}
}
If you have multiple sources of randomness, such as a value read from /dev/random under Linux, or a white noise generator of some description, or the average number of milliseconds between keypresses the last time a user ran this program, you could use those as extra inputs:
std::seed_seq seq{time(0), valFromDevRandom(), getWhiteNoise(), avgMillis()};
but I doubt constants are the way to go, since they add no randomness to the equation.
According The C++11 standard (in section 26.5.7.1.8),seed_seq can generate a sequence which is likely generated by a hash function, uniformly and randomly in the range.
I try to answer the below questions:
Q1 "Since a seed_seq can be initialized with an arbitrary number of integers, what's the significance of the length of its initializer list? If I want to produce seeds for 100 random generators, do I need to initialize my seed_seq with 100 integers?"
A1. You needn't initialize seed_seq with a lot integers. Even seed_seq initialized by one random integer, the generated sequence keep the randomness. But you initialize seed_seq with more integers and in the wider range, The generated sequence is more hardly "collide" by attackers.
Q2. "If the length of the initializer list doesn't have to match the number of seeds I intend to generate, is it OK to initialize a seed_seq with just one integer and then use it to produce a large number of seeds?"
A2. Yes, it is OK to initialize a seed_seq with just one integer if you don't need cryptographically secure level.
Q3. "How about initializing with no integers, i.e. using the default constructor? (This means I'd get the same seeds every time, of course.)"
A3. You will get the identical sequences by the default constructed seed_seq runs more. Thus it will became a security hole.
Q4. "If it's OK to construct a seed_seq from a single integer and then generate lots of seeds from it, what's the benefit of using seed_seq instead of an ordinary random generator? Why not just construct a std::mt19937 from that single integer and use that to produce seed values for other generators?"
A4. seed_seq is a light-weight algorithm, only iterates the filled sequence 3 times. I guess you can use other random generator instead of seed_seq.

Is the <random> library in c++11 portable?

Is the library in c++11 portable? I have avoided rand() because I heard it wasn't portable.
How do you define "portable"?
If by "portable", you mean "will produce binary identical sequences of random numbers given the same input", then yes, rand isn't portable. And yes, the C++ random generators are portable (most of them. Not std::default_random_engine or std::random_device), because they implement specific algorithms. rand is allowed to be anything, as long as it's not entirely unlike a random number generator.
That being said, as #PeteBecker pointed out, the distributions themselves are not so well-defined. So while std::mt19937 will produce the same sequence of values for a given seed, different std::uniform_int_distributions can give different values for the same input sequence and range.
Of course, if you need consistency, you can always define your own distribution.
The random number engines described in <random> have explicit requirements for their algorithms to ensure portability. The distributions do not.
You can generate "identical sequences of random numbers given the same input" (from #Nicol Bolas) with std::mt19937 (Mersenne Twister) for example. You definitely couldn't do that with rand() which was quite annoying.
Related questions:
Does the C++11 standard guarantee identical random numbers for the same seed across implementations?
Consistent pseudo-random numbers across platforms

Does the C++11 standard guarantee identical random numbers for the same seed across implementations?

For example if I instantiate a std::mt19937 with the exact same seed and parameters under GCC and under MSVC, should I get the same sequence of random numbers? If so I assume this property would hold for mersenne_twister_engine in general since mt19937 is just one with specific parameters. This is not true for rand() in C. It looks like the standard documents the transformations applied in terms of specific code, so I suspect it should always be the same, but the devil is in the details...
For the new random number engines, yes, for the same seed and parameters you'll get the same sequence of values on all platforms. For rand(), no. You also don't have that guarantee with random number distributions, even when they are fed the same sequence of input values.