Algorithm to select a single, random combination of values?

后端 未结 7 1062
傲寒
傲寒 2020-11-22 14:04

Say I have y distinct values and I want to select x of them at random. What\'s an efficient algorithm for doing this? I could just call rand(

7条回答
  •  盖世英雄少女心
    2020-11-22 14:52

    If, for example, you have 2^64 distinct values, you can use a symmetric key algorithm (with a 64 bits block) to quickly reshuffle all combinations. (for example Blowfish).

    for(i=0; i

    This is not random in the pure sense but can be useful for your purpose. If you want to work with arbitrary # of distinct values following cryptographic techniques you can but it's more complex.

提交回复
热议问题