Node: Generate 6 digits random number using crypto.randomBytes

前端 未结 6 1413
陌清茗
陌清茗 2020-12-16 20:44

What is the correct way to generate exact value from 0 to 999999 randomly since 1000000 is not a power of 2?

This is my approa

6条回答
  •  隐瞒了意图╮
    2020-12-16 21:33

    There are several way to extract random numbers in a range from random bits. Some common ones are described in NIST Special Publication 800-90A revision 1: Recommendation for Random Number Generation Using Deterministic Random Bit Generators

    Although this standard is about deterministic random bit generations there is a helpful appendix called A.5 Converting Random Bits into a Random Number which describes three useful methods.

    The methods described are:

    • A.5.1 The Simple Discard Method
    • A.5.2 The Complex Discard Method
    • A.5.3 The Simple Modular Method

    The first two of them are not deterministic with regards to running time but generate a number with no bias at all. They are based on rejection sampling.

    The complex discard method discusses a more optimal scheme for generating large quantities of random numbers in a range. I think it is too complex for almost any normal use; I would look at the Optimized Simple Discard method described below if you require additional efficiency instead.

    The Simple Modular Method is time constant and deterministic but has non-zero (but negligible) bias. It requires a relatively large amount of additional randomness to achieve the negligible bias though; basically to have a bias of one out of 2^128 you need 128 bits on top of the bit size of the range required. This is probably not the method to choose for smaller numbers.

    Your algorithm is clearly a version of the Simple Discard Method (more generally called "rejection sampling"), so it is fine.


    I've myself thought of a very efficient algorithm based on the Simple Discard Method called the "Optimized Simple Discard Method" or RNG-BC where "BC" stands for "binary compare". It is based on the observation that comparison only looks at the most significant bits, which means that the least significant bits should still be considered random and can therefore be reused. Beware that this method has not been officially peer reviewed; I do present an informal proof of equivalence with the Simple Discard Method.


    Of course you should rather use a generic method that is efficient given any value of N. In that case the Complex Discard Method or Simple Modular Method should be considered over the Simple Discard Method. There are other, much more complex algorithms that are even more efficient, but generally you're fine when using either of these two.

    Note that it is often beneficial to first check if N is a power of two when generating a random in the range [0, N). If N is a power of two then there is no need to use any of these possibly expensive computations; just use the bits you need from the random bit or byte generator.

提交回复
热议问题