Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

Trying to generate random numbers in C, rand() doesn't generate different numbers each time i compile the code, can anyone tell me how to use srand() or tell any other method for generating.

like image 683
Jehoshuah Avatar asked Feb 08 '11 08:02

Jehoshuah


People also ask

How do you generate random numbers in Linux?

Example: echo $(($RANDOM%10)) #This will display a random integer between 0 and 9. echo $(($RANDOM%11)) #This will display a random integer between 0 and 10.

How do you randomize two numbers in C?

In the c programming language, there is an inbuilt method to take care of this. srand() and rand() functions are used to generate random numbers.

What does random () do in C?

Description. The C library function int rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.


2 Answers

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.
like image 104
gspr Avatar answered Oct 17 '22 11:10

gspr


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).

like image 26
Matěj Zábský Avatar answered Oct 17 '22 09:10

Matěj Zábský