fork download
  1. // Problem 1: Linear Congruential Generator (LCG)
  2.  
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5.  
  6. int main() {
  7. int a = 5, c = 3, m = 16;
  8. int X = 1;
  9.  
  10. cout << "Random numbers:\n";
  11. for (int i = 1; i <= 10; i++) {
  12. X = (a * X + c) % m;
  13. cout << X << " " ;
  14. }
  15. cout << "\nNormalized values in [0,1]:\n";
  16.  
  17. X = 1; // reset seed to regenerate for normalization
  18. for (int i = 1; i <= 10; i++) {
  19. X = (a * X + c) % m;
  20. double r = (double)X / m;
  21. cout << r << " ";
  22. }
  23. cout << endl;
  24.  
  25. return 0;
  26. }
  27.  
  28. /*
  29. Expected Output:
  30.  
  31. 1. Sequence of generated random numbers:
  32. = 8 11 10 5 12 15 14 9 0 3
  33.  
  34. 2. Normalized to the range [0, 1):
  35. = 0.5 0.6875 0.625 0.3125 0.75 0.9375 0.875 0.5625 0 0.1875
  36.  
  37. 3. Are there any repetitions in the sequence?
  38. = No
  39. */
  40.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Random numbers:
8 11 10 5 12 15 14 9 0 3 
Normalized values in [0,1]:
0.5 0.6875 0.625 0.3125 0.75 0.9375 0.875 0.5625 0 0.1875