Efficient random permutation of n-set-bits

后端 未结 5 784
天命终不由人
天命终不由人 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条回答
  •  旧时难觅i
    2021-01-21 14:22

    I would use solution number 3, generate the i-th permutation.
    But do you need to generate the first i-1 ones?

    You can do it a bit faster than that with kind of divide and conquer method proposed here: Returning i-th combination of a bit array and maybe you can improve the solution a bit

提交回复
热议问题