Hacker News new | ask | show | jobs
by ascar 1878 days ago
If you want a simple pseudo random sequence of N that just looks random (pseudocode):

  int start = getRandomInt(seed) % N;
  double k = N / 1.618;
  k = k % 2 == 0 ? k - 1 : k; // k and N must be coprime
  int nextElement = k * start % N;
  start++;
  
This will jump wildly across your sequence and visit all elements (as k and N are chosen coprime). No need to store a full array. Is it statistically random? Of course not, e.g. you will never see two values close to each other right after another.
1 comments

Your algorithm fails to choose k and N coprime for about 19% of N, such as N = 6, 10, 14, 15, 25, 27, 35, 36, 45, 54, 55, 65, 66, 75, 84, 85, 90, 93, 95 ….