programming-examples/c/Numerical/C Program to Implement the linear congruential generator for Pseudo Random Number Generation.c

39 lines
659 B
C
Raw Normal View History

2019-11-15 12:59:38 +01:00
#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;
}