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

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

    Here is some pseudocode in my own made-up language:

    function f(array a)
        array newArray
        while a.size() == 0
            int position = randomNumber(1 to a.size())
            int removedNumber = a[position]
            a.remove(position)
            newArray.insertAtEnd(removedNumber)
        end while
        return newArray
    

提交回复
热议问题