This is a C Program to generate random numbers using Linear Congruential Generator. The linear congruential generator is a very simple example of a random number generator. All linear congruential generators use this formula:

r

Where:

r0 is a seed.

r1, r2, r3, …, are the random numbers.

a, c, m are constants.

r

_{{n + 1}}= a x r_{n}+ c mod mWhere:

r0 is a seed.

r1, r2, r3, …, are the random numbers.

a, c, m are constants.

Here is source code of the C Program to Implement the linear congruential generator for Pseudo Random Number Generation. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`#include <stdio.h>`

`/* always assuming int is at least 32 bits */`

int rand();

int rseed = 0;

inline void srand(int x) {

rseed = x;

`}`

`#ifndef MS_RAND`

`#define RAND_MAX ((1U << 31) - 1)`

inline int rand() {

return rseed = (rseed * 1103515245 + 12345) & RAND_MAX;

`}`

`#else /* MS rand */`

`#define RAND_MAX_32 ((1U << 31) - 1)`

`#define RAND_MAX ((1U << 15) - 1)`

inline int rand()

`{`

return (rseed = (rseed * 214013 + 2531011) & RAND_MAX_32) >> 16;

`}`

`#endif/* MS_RAND */`

int main() {

int i;

printf("rand max is %d\n", RAND_MAX);

for (i = 0; i < 10; i++)

printf("%d\n", rand());

return 0;

`}`

Output:

$ gcc LCG.c $ ./a.out rand max is 2147483647 12345 1406932606 654583775 1449466924 229283573 1109335178 1051550459 1293799192 794471793 551188310

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data Structures and Algorithms.