fork download
  1. // Problem 1: (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:\n";
  16.  
  17. X = 1;
  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. Q1. Do you see repeating numbers?
  30. = Yes. For example, 5 and 12 appear again later in the sequence.
  31.  
  32. Q2. How long before it repeats?
  33. = In this setup (a=5, c=3, m=16), the sequence repeats after 8 numbers.
  34.   This repeating length is called the "period" of the generator.
  35. */
  36.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Random numbers:
8 11 10 5 12 15 14 9 0 3 
Normalized values:
0.5 0.6875 0.625 0.3125 0.75 0.9375 0.875 0.5625 0 0.1875