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

后端 未结 10 1966
迷失自我
迷失自我 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:57

    It's not possible to return the values without storing the results of the original function somewhere. Reasoning:

    Your random number generator tells you to return these values from the original sequence: 5th, 11th, 3rd.

    So you skip the first four values, return the 5th, skip another 5, return the 11th ... now how do you return the 3rd without saving it somewhere?

    The closest thing you could get away with is creating a list and append all the values which you skip but that sound very awkward and probably not worth the effort. Also, it would be very slow in the case where the shuffling algorithm returns a very big and then a very small value (in which case you would effectively copy most values into the list, first, which you want to avoid).

    I rest my case.

提交回复
热议问题