fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. // LCG parameters
  5. int a = 5;
  6. int c = 3;
  7. int m = 16;
  8. int X = 1; // Seed value (X0)
  9. int n = 10; // Number of values to generate
  10.  
  11. int sequence[10]; // Store generated numbers
  12. float normalized[10]; // Store normalized values
  13.  
  14. printf("Generated LCG sequence:\n");
  15. for (int i = 0; i < n; i++) {
  16. sequence[i] = X;
  17. normalized[i] = (float)X / m;
  18. printf("X_%d = %d\tNormalized = %.4f\n", i, X, normalized[i]);
  19.  
  20. // Generate next number in the sequence
  21. X = (a * X + c) % m;
  22. }
  23.  
  24. // Check for repetitions
  25. int hasRepetition = 0;
  26. for (int i = 0; i < n; i++) {
  27. for (int j = i + 1; j < n; j++) {
  28. if (sequence[i] == sequence[j]) {
  29. hasRepetition = 1;
  30. break;
  31. }
  32. }
  33. if (hasRepetition) break;
  34. }
  35.  
  36. if (hasRepetition)
  37. printf("\nThere are repetitions in the sequence.\n");
  38. else
  39. printf("\nThere are no repetitions in the sequence.\n");
  40.  
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Generated LCG sequence:
X_0 = 1	Normalized = 0.0625
X_1 = 8	Normalized = 0.5000
X_2 = 11	Normalized = 0.6875
X_3 = 10	Normalized = 0.6250
X_4 = 5	Normalized = 0.3125
X_5 = 12	Normalized = 0.7500
X_6 = 15	Normalized = 0.9375
X_7 = 14	Normalized = 0.8750
X_8 = 9	Normalized = 0.5625
X_9 = 0	Normalized = 0.0000

There are no repetitions in the sequence.