开发者

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

Trying to generate random numbers in C, rand() doesn't gener开发者_如何学Cate different numbers each time i compile the code, can anyone tell me how to use srand() or tell any other method for generating.


In order to generate a sequence of pseudorandom numbers, the generator needs to be seeded. The seed fully determines the sequence of numbers that will be produced. In C, you seed with srand, as you indicate. According to the srand(3) man page, no explicit seeding implies that the generator will use 1 as a seed. This expains why you always see the same numbers (but do remember that the sequence itself is pretty random, with quality depending on the generator used, even though the sequence is the same each time).

User mzabsky points out that one way to get a seed that feels random to a human user is to seed with time. Another common method (which I just saw that mzabsky also points out - sorry) is to seed the generator with the contents of the system's random number generator, which draws from an entropy pool fed by things such as mouse movement, disk timings etc. You can't draw a lot of randomness from the system generator, as it won't be able to gather enough entropy. But if you just draw a seed from it, you'll have chosen at random a sequence of random numbers in your program. Here's an example of how to do that in C on Linux:

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

In light of Conrad Meyer's answer, I thought I'd elaborate a bit more. I'd divide the use of random numbers into three categories:

  1. Variation. If you use random numbers to create seemingly random or varied behavior in for example a game, you don't need to think very hard about the topic, or about choosing a proper seed. Seed with time, and look at some other solution if this turns out not to be good enough. Even relatively bad RNGs will look random enough in this scenario.
  2. Scientific simulations. If you use random numbers for scientific work, such as Monte Carlo calculations, you need to take care to choose a good generator. Your seed should be fixed (or user-changeable). You don't want variation (in the sense above); you want deterministic behavior but good randomness.
  3. Cryptography. You'll want to be extremely careful. This is probably out of the scope of this thread.


This is commonly used solution:

srand ( time(NULL) );

Execution of all C code is deterministic, so you have to bring in something that is different every time you call the srand. In this case it is time.

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


If you are using an OS that does not provide /dev/random then use something like what is shown below

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

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

To improve the seed- you can combine (maybe using MD5 or a checksum algorithm) time product shown above with a 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);


Be careful; the rand(3) manpage on linux notes that rand() implementations on some platforms do not give good randomness on the lower-order bits. For this reason, you might want to use a library to acquire real random numbers. Glib provides useful functions like g_random_int_range() which may better suite your purpose.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜