C++ need a good technique for seeding rand() that does not use time()

c random number in range
c++ random number between 1 and 10
c++ random number between 0 and 1
rand() c
c random number between 0 and 1
c++ generate random number between 1 and 10
srand(time(null)) function in c++

I have a bash script that starts many client processes. These are AI game players that I'm using to test a game with many players, on the order of 400 connections.

The problem I'm having is that the AI player uses

srand( time(nullptr) );

But if all the players start at approximately the same time, they will frequently receive the same time() value, which will mean that they are all on the same rand() sequence.

Part of the testing process is to ensure that if lots of clients try to connect at approximately the same time, the server can handle it.

I had considered using something like

srand( (int) this );

Or similar, banking on the idea that each instance has a unique memory address.

Is there another better way?

You use a random number seed if and only if you want reproducible results. This can be handy for things like map generation where you want the map to be randomized, but you want it to be predictably random based on the seed.

For most cases you don't want that, you want actually random numbers, and the best way to do that is through the Standard Library generator functions:

#include <random>

std::random_device rd;
std::map<int, int> hist;
std::uniform_int_distribution<int> dist(0, 5);

int random_die_roll = dist(rd);

No seed is required nor recommended in this case. The "random device" goes about seeding the PRNG (pseudo random number generator) properly to ensure unpredictable results.

Again, DO NOT use srand(time(NULL)) because it's a very old, very bad method for initializing random numbers and it's highly predictable. Spinning through a million possible seeds to find matching output is trivial on modern computers.

rand() and srand() in C/C++, We use cookies to ensure you have the best browsing experience on our website​. rand() function is used in C to generate random numbers. It should not be repeatedly seeded, or reseeded every time you wish to generate a new batch of Please write comments if you find anything incorrect, or you want to share more​  But if all the players start at approximately the same time, they will frequently receive the same time() value, which will mean that they are all on the same rand() sequence. Part of the testing process is to ensure that if lots of clients try to connect at approximately the same time, the server can handle it.

Use a random seed to a pseudorandom generator.

std::random_device is expensive random data. (expensive as in slow) You use that to seed a prng algorithm. mt19937 is the last prng algorithm you will ever need.

You can optionally follow that up by feeding it through a distribution if your needs require it. i.e. if you need values in a certain range other than what the generator provides.

std::random_device rd;
std::mt19937 generator(rd());

Random Number Generation Funcitons, Computers can be used to simulate the generation of random numbers with the are not truly "random" because a mathematical formula is used to generate the Since the same sequence is generated each time the seed remains the same, numbers within a specified range, you need to manipulate the rand( ) function. The C library function void srand(unsigned int seed) seeds the random number generator used by the function rand. This function does not return any value

These days rand() and srand() are obsolete.

The generally accepted method is to seed a pseudo random number generator from the std::random_device. On platforms that provide non-deterministic random sources the std::random_device is required to use them to provide high quality random numbers.

However it can be slow or even block while gathering enough entropy. For this reason it is generally only used to provide the seed.

A high quality but efficient random engine is the mersenne twister provided by the standard library:

std::mt19937& random_generator()
    thread_local static std::mt19937 mt{std::random_device{}()};
    return mt;

template<typename Number>
Number random_number(Number from, Number to)
        "Parameters must be integer or floating point numbers");

    using Distribution = typename std::conditional

    thread_local static Distribution dist;

    return dist(random_generator(), typename Distribution::param_type{from, to});

C/Randomization, If you want random values in a C program, there are three typical ways of getting them, that rand returns, but it should be good enough for casual use, and has the advantage If you want to get different sequences, you need to seed the random number generator using srand. 1 int d6(void) { 2 return rand() % 6 + 1; 3 }. rand () function is used in C to generate random numbers. If we generate a sequence of random number with rand () function, it will create the same sequence again and again every time program runs. Say if we are generating 5 random numbers in C with the help of rand () in a loop, then every time we compile and run the program our output must be

Guide on a Random Number Generator C++: The Use of C++ Srand, Such a generator can have the starting number (the seed) and the Learning how to generate random numbers in C++ is not difficult (but you With the C++ rand() method, you can return a positive number within the A very useful tip to make C++ generate random numbers is to use the time() method. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767. Declaration Following is the declaration for rand() function.

How do you control seeds for generating random numbers and , "I do not consider seeds value as a relevant factor that may influence the For example, when you want to compare distinct variants of the same method or algorithms in a fair manner, I will use the same random seed based on time. It is much better than algorithm implemented in old C with functions rand() and srand(). Hydroseeding is a process also known as hydraulic mulch seeding or hydro mulching, and it's a technique that sprays a seed mixture over an area in order to plant grass or other seeds there. When done correctly in the right conditions, the process can be more efficient and affordable than traditional, time-consuming methods, such as tilling the

5.9, That's a good thought, but if we need a random number to generate random Here's the same program as above, using a call to time() as the seed: Generally, we do not want random numbers between 0 and Most implementations of rand() use a method called a Linear Congruential Generator (​LCG). If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand. In order to generate random-like numbers, srand is usually initialized to some distinctive runtime value, like the value returned by function time (declared in header <ctime> ).

  • std::random_device
  • submit as a response please
  • Possible duplicate of stackoverflow.com/questions/45069219/…
  • WARNING: Using rand() is considered harmful and you’re strongly encouraged to use an appropriate random number generator facility in the Standard Library that produces actually random values. Your use of time(NULL) as a random number seed means that this will produce identical results if run in the same second, and on many platforms rand() is barely random at all.
  • "they will frequently receive the same time" - or, they might even be able to choose good starting times.
  • "You use a random number seed if and only if you want reproducible results." That seems a bit strong. While std::random_device is decently fast (at least on gcc on Linux), you would not run a Monte-Carlo simulation based solely on it, but rather use it to seed a Mersenne twister.
  • Then fix your answer; that (speed) is another reason to use a seeded prng. Maybe seeded from a random device, but seeded.
  • std::random_device can be deterministic if the implementation doesn't have features (or exploit hardware features) that achieve non-determinism. In such a case, it is effectively the same as using a seed. Something worth checking (e.g. reading documentation for your compiler/library) if you really need unpredictability.
  • You talk about std::random_device seeding the PRNG but there is no PRNG in your code. Also this code is expensive and may even block while the device gathers more entropy. I don't see this as a good general solution which would be to seed a PRNG with the std::random_device rather than use it directly.
  • Thank you everyone... I love the Mersenne numbers, and didn't notice this chunk of the std lib. Very helpful! Thank you again, it works perfectly.
  • expensive (on the order of milliseconds or microseconds) is acceptable if the result is correct, even if it's blocking in a multithreading context
  • "if the result is correct". Considering it would take a team of data scientists with PhDs to tell the difference between an actual random data sample and one generated from mt19937, i would ask why you wouldn't want fast, repeatable (if needed), and correct. The only reason (imo) to not use it is if you need a cryptographically secure algorithm.