How to "randomize ()" random numbers in C (Linux)?

Trying to generate random numbers in C, rand () does not generate different numbers every time I compile the code, can someone tell me how to use srand () or tell some other method to generate.

+6
c
source share
4 answers

To generate a pseudorandom sequence, the generator must be seeded . A seed fully defines the sequence of numbers to be created. In C, you sow srand as you specify. According to the srand(3) man page, without explicit seeding, it is assumed that the generator will use 1 as a seed. This leads to the fact that you always see the same numbers (but remember that the sequence itself is quite random, the quality depends on the generator used, although the sequence is the same).

User mzabsky points out that one way to get a seed that seems random to the user is with time. Another common method (which I just saw that mzabsky also indicates - sorry) is to seed the generator with the contents of the random number generator of the system, which is extracted from the entropy pool fed by things such as mouse movement, disk time intervals, etc. . You cannot extract a lot of randomness from the generator of the system, since it cannot collect enough entropy. But if you simply extract the seed from it, you would randomly choose a sequence of random numbers in your program. Here is an example of how to do this in C on Linux:

 unsigned int seed; FILE* urandom = fopen("/dev/urandom", "r"); fread(&seed, sizeof(int), 1, urandom); fclose(urandom); srand(seed); 

In response to Conrad Meyer, I thought I would do some more work. I would divide the use of random numbers into three categories:

  • . . If you use random numbers to create seemingly random or varied behaviors, such as in a game, you don't need to think too much about the topic or choosing the right seed. Seed over time, and look at another solution if it turns out to be not good enough. Even relatively bad RNGs will look quite random in this scenario.
  • Scientific simulations. If you use random numbers for scientific work, such as calculations in Monte Carlo, you need to take care of choosing a good generator. Your seed must be corrected (or modified by the user). You do not want change (in the sense of the above); you need deterministic behavior, but a good coincidence.
  • Cryptography. You will be very careful. This is probably beyond the scope of this topic.
+11
source share

This is a commonly used solution:

 srand ( time(NULL) ); 

The execution of all C code is deterministic, so each time you call srand you must enter something else. In this case, the time has come.

Or you can read data from /dev/random (open it like any other file).

+8
source share

If you are using an OS that does not provide / dev / random, then use something like the one shown below

timeval t1;
gettimeofday(&t1, NULL);
srand(t1.tv_usec * t1.tv_sec);

This piece of code can be easily ported to another OS.

To improve the seeds, you can combine (possibly using MD5 or the checksum algorithm) the time product shown above with the MAC address of the host machine.

timeval t1;
gettimeofday(&t1, NULL);
unsigned int seed = t1.tv_usec * t1.tv_sec;

unsigned char mac_addr [6];
getMAC (& mac_addr);
improveSeedWithMAC (& seed, mac_addr); // MD5 or checksum ...

srand (seed);

+4
source share

Be careful; rand(3) manpage in Linux notes that the implementation of rand() on some platforms does not give a good chance for low bits. For this reason, you might want to use the library to get real random numbers. Glib provides useful functions like g_random_int_range() that can make better use of your target.

+3
source share

All Articles