Efficient random permutation of n-set-bits

后端 未结 5 780
天命终不由人
天命终不由人 2021-01-21 13:52

For the problem of producing a bit-pattern with exactly n set bits, I know of two practical methods, but they both have limitations I\'m not happy with.

Fir

5条回答
  •  温柔的废话
    2021-01-21 14:31

    [Expanding my comment:] If you only have a little raw entropy available, then use a PRNG to stretch it further. You only need enough raw entropy to seed a PRNG. Use the PRNG to do the actual shuffle, not the raw entropy. For the next shuffle reseed the PRNG with some more raw entropy. That spreads out the raw entropy and makes less of a demand on your entropy source.

    If you know exactly the range of numbers you need out of the PRNG, then you can, carefully, set up your own LCG PRNG to cover the appropriate range while needing the minimum entropy to seed it.

    ETA: In C++there is a next_permutation() method. Try using that. See std::next_permutation Implementation Explanation for more.

提交回复
热议问题