<random> is useless?

This header is trash. This only return the same numbers, everytime that I run. What is wrong? How can I fix it? I have to use "rand()" forever?
headers don't return numbers, functions do. what function are you calling?

Incidentally, rand() is getting special wording in C++14 discouraging its use (but not yet deprecation)
You need to seed the pseudorandom number generator with a different seed in order to get a different sequence of numbers.
As mentioned, make sure you're seeding. I usually use mersenne twister if I use <random>. It gives 'decent randomness' for games and stuff. http://www.cplusplus.com/reference/random/mt19937/

Do remember to seed. There is a complete example (seeding and twister) here: http://www.cplusplus.com/reference/random/mersenne_twister_engine/seed/

@Cubbi - Interesting...
I use "random_device"
How are you using it? This seems to work http://ideone.com/U6fAX7
If you're using MinGW GCC, you'll get the same sequence of numbers every time:
http://stackoverflow.com/questions/18880654/why-do-i-get-same-sequence-for-everyrun-with-stdrandom-device-with-mingw-gcc4

It sucks, I know.
For now, I would work around it by using a time seed or something of that sort.
(There's also another workaround in the link above.)
Last edited on
Unseeded rand() has a purpose. If you're debugging a large and complicated program that is associated with random numbers, it's advantageous to have the same sequence repeat itself. Even if you're not debugging, only testing, it can come in handy.
Unseeded rand() has a purpose. If you're debugging a large and complicated program that is associated with random numbers, it's advantageous to have the same sequence repeat itself.


In that case I would just give it a fixed seed. IE srand(0);. That way you're guaranteed to get the same sequence.

Unseeded is uninitialized. I never want to use anything uninitialized.
There was a definite disconnect with what I wrote to what I was thinking. I really meant to say that a repetitive sequence of random numbers has a purpose.
Ah. In that case, i agree completely. =)
> Unseeded is uninitialized.

Unseeded is default initialized: initialized as if a call std::srand(1) ; had been made first.
Really? I stand corrected then.

If you're using MinGW GCC, you'll get the same sequence of numbers every time:
http://stackoverflow.com/questions/18880654/why-do-i-get-same-sequence-for-everyrun-with-stdrandom-device-with-mingw-gcc4

It sucks, I know.
For now, I would work around it by using a time seed or something of that sort.
(There's also another workaround in the link above.)


Oh, dammit! Thanks...
How about using CryptGenRandom() on windows or reading from /dev/urandom device file on *nix ?

These are cryptographically secure pseudo random number generators used by the operating system itself, probably much better than any <random> implementation.

There is no need to "seed" them.

http://en.wikipedia.org/wiki/CryptGenRandom
http://msdn.microsoft.com/en-us/library/windows/desktop/aa379942%28v=vs.85%29.aspx
http://en.wikipedia.org/wiki//dev/random

Also there is RDRAND Intel CPU instruction :)
http://stackoverflow.com/questions/21420219/how-to-get-cryptographically-strong-random-bytes-with-windows-apis/21447428#21447428
Last edited on
Still you can exhaust entropy pool even with calls to urandom impairing other users of the machine. YOu should use it with caution. Also they are unusable if you will need to repeat same sequence of random numbers again.

So, learn how each of these methods work, theis upsides and downsides and use what you think better suits you.
Topic archived. No new replies allowed.