2

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?

MvG
  • 57,380
  • 22
  • 148
  • 276
baozi
  • 679
  • 10
  • 30
  • It should be ok to call it just once. Why would that cause correlation effects? – jogojapan Jan 24 '14 at 07:58
  • 1
    Are you trying to protect against inadequacies of a particular PRNG in your domain? If not, just seed once, use the first N for the first sequence, and the next N for the second. – juanchopanza Jan 24 '14 at 08:02
  • @jogojapan After reading all the posts, i realized only one seed is needed.(one seed guarantees independence) – baozi Jan 27 '14 at 22:38

3 Answers3

6

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 2
    A must-see video if you need to work with random numbers: http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful – MikMik Jan 24 '14 at 10:59
0

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.

Community
  • 1
  • 1
MvG
  • 57,380
  • 22
  • 148
  • 276
-1

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

erlc
  • 652
  • 1
  • 6
  • 11
  • There is *no* reason to use two devices. This is bad advice. Even worse because you don’t seed them – they will produce *anything but* independent distributions. – Konrad Rudolph Jan 24 '14 at 09:56
  • @KonradRudolph: You don't seed a random number *devive*. It's most likely based on some entropy pool provided by the kernel, e.g. `/dev/urandom` on Linux. See [its manual](http://en.cppreference.com/w/cpp/numeric/random/random_device) and that of [its constructor](http://en.cppreference.com/w/cpp/numeric/random/random_device/random_device). – MvG Jan 24 '14 at 10:05
  • @MvG Ah, true of course (I was confused because you wouldn’t use a random device here anyway, you’d use a generator). The comment still stands – the answer is wrong (so is yours by the way – for uniformly generated random numbers, “one sequence” is the same as “two sequences”). – Konrad Rudolph Jan 24 '14 at 10:13
  • @KonradRudolph: While I agree for true random numbers, we are talking about pseudo-random numbers. And there it might be that a single sequence has different properties than two do. For example, in a cryptographic context, one recipient of those numbers might be able to request a bunch of numbers and gain enough knowledge about the internal state of the prng to predict the other sequence. Whether any of this is relevant in practice depends, and secure prng are a different matter altogether. The point is, there *might* be a difference, however small. – MvG Jan 24 '14 at 10:22
  • @MvG Your intuition is leading you astray. Even for imperfect generators it will generally **not** help to take two instead of one. It’s definitely not true for the state of the art generators. If anything you *lose* independence that way. The predictiveness of a generator is unrelated to that, because that also applies to a single distribution, and we’re talking about independence of two distributions. – Konrad Rudolph Jan 24 '14 at 10:49