Iterator to produce unique random order?

后端 未结 2 1814
离开以前
离开以前 2020-12-10 22:06

The problem is stated as follows, we have a very large number of items which are traversed through an iterator pattern (which dynamicaly constructs or fetches) the requested

2条回答
  •  轮回少年
    2020-12-10 22:19

    Encryption is reversible, hence an encryption is a one-to-one mapping from a set onto itself.

    Pick a block cypher with a large enough block size to cover the number of items you have.

    Encrypt the numbers 0, 1, 2, 3, 4, ... This will give you a non-repeating ordered list of numbers up to 2^(block size).

    If the encrypted number is too large, ignore it. If the encrypted number is within the size of your item list, then pick that item. Repeat for however many items you need.

    A cypher with variable block-size (like the Hasty Pudding cypher) will reduce the number of misses.

提交回复
热议问题