Looking for an algorithm to spit out a sequence of numbers in a (pseudo) random order

后端 未结 10 1989
迷失自我
迷失自我 2021-02-03 15:33

Suppose I have a sequence of numbers: {n, n+1, n+2, ... n + m}

Without storing the numbers ahead of time I want to create a function f(), which given the sequence {1,2,3

10条回答
  •  你的背包
    2021-02-03 15:50

    Your input is described by f(x) => x + 9, or more generally f(x) => n - 1 + x as x starts at 1.

    You link to another question which describes a function r(x) which maps x to a shuffled value, 0 <= r(x) <= m.

    so f(r(x) + 1) or (r(x) + n)should give you the value you want.

    For small m, you also should be able to find seeds of a standard random number generator by trail and error which then generate m+1 distinct values when taken mod m+1 if you don't want to code your own generator.

提交回复
热议问题