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

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

    You can generate a permutation of the first n integers by using a block cipher and xor folding, as per my previous answer.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-03 15:49

    You can fit a polynomial to the selected sequence; I'd guess that's what your coworker showed you. It won't save space compared to just remembering the permutation, though.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-03 15:53

    Your question is a bit confusing, as it sounds like you want to get all of the original sequence back, but then you have both 4 and 8 mapping to 10, and nothing mapping to 12.

    If you actually meant that to be a 1:1 mapping, then what you are looking for is a random permutation of the original set. There are ways to do this with or without collecting up the set first (but you'll need something that generates it, or keep track of where you are).

    Also, note that n is not important. You can always use 0,1,2,...m and then add n to everything if needed.

    Assuming I've interpreted this correctly and you are actually looking for a shuffle algorithm (i.e. random permutation, called shuffles by analogy to shuffling a deck of cards), have a look at Fisher-Yates

    [Edit] Ok, based on your update, the problem you face is this: You don't want to encode the permutation explicitly, but you must encode it somehow in order to construct f. The easiest way is just to actually store the permuted indices in an array, but if you don't want to do that for some reason (e.g. too big), you can encode it in various ways. There is no free lunch though, as there are information theoretic limits on how simple this can be. Anyway, you can get some ideas from looking up work on "encoding permutations" for example something like this paper

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题