Write c program for random numbers generation. The basic function rand() can be used to generate a pseudo random integer value between 0 and RAND_MAX (0 and RAND_MAX included). The value of RAND_MAX is implementation dependent.

sample c program for random numbers generation

Notes: The usage of rand() is not recommended for serious random number generation needs, like cryptography.

C program for random numbers generation based on xorshift

A better alternative of rand() function, is Xorshift, a class of pseudo-random number generators discovered by George Marsaglia. The Xorshift generator is a fastest non-cryptographic and secure random number generators.

c program for random numbers generation without using rand() function

The program is a simple modification of linear feedback shift registers (https://en.wikipedia.org/wiki/Linear-feedback_shift_register). This method uses the fact that when we allocate a memory, system returns the address of the memory address.

Note: The above program will generate random numbers from 0 to max_range_from_zero. if we want to generate random number between a range then we can modified the program as below.




Related Contents to follow