ChaosNuggets

Random number generator example (linear congruential generator)

Mar 3rd, 2023 (edited)
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. // I am well aware that this code is not good (no static, not considering overflows on some machines) and is not optimal (not using & for fast modulus). I'm just trying to make my code as clear and simple as possible.
  2. #include <stdio.h>
  3.  
  4. #define RAND_MAX 2048
  5.  
  6. int rseed = 0; // Global variable: course standards wants to know your location
  7. void srand(int);
  8. int rand();
  9.  
  10. int main()
  11. {
  12.     int i;
  13.    
  14.     srand(1000);
  15.     for (i = 0; i < 100; i++)
  16.     {
  17.         printf("%d\n", rand());
  18.     }
  19.    
  20.     return 0;
  21. }
  22.  
  23. void srand(int seed)
  24. {
  25.     rseed = seed;
  26. }
  27.  
  28. int rand()
  29. {
  30.     rseed = (rseed * 69420 + 21) % (RAND_MAX + 1);
  31.     return rseed;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment