pseudo random distribution which guarantees all possible permutations of value sequence - C++

前端 未结 3 1924
天命终不由人
天命终不由人 2021-01-13 16:53

Random question.

I am attempting to create a program which would generate a pseudo-random distribution. I am trying to find the right pseudo-random algorithm for my

3条回答
  •  遥遥无期
    2021-01-13 17:48

    There are M**N sequences of N numbers between 0 and M-1. You can imagine writing all of them one after the other in a (pseudorandom) sequence and placing your read pointer randomly in the resulting loop of N*(M**N) numbers between 0 and M-1...

    def output(input):
        total_length = N*(M**N)
        index = input % total_length
        permutation_index = shuffle(index / N, M**N)
        element = input % N
        return (permutation_index / (N**element)) % M
    

    Of course for every permutation of N elements between 0 and M-1 there is a sequence of N consecutive inputs that produces it (just un-shuffle the permutation index). I'd also say (just using symmetry reasoning) that given any starting input the output of next N elements is equally probable (each number and each sequence of N numbers is equally represented in the total period).

提交回复
热议问题