How do I scale down numbers from rand()?

后端 未结 9 1960
小蘑菇
小蘑菇 2020-11-27 14:38

The following code outputs a random number each second:

int main ()
{
    srand(time(NULL)); // Seeds number generator with execution time.

    while (true)         


        
9条回答
  •  借酒劲吻你
    2020-11-27 15:00

    You can do

    cout << rawRand % 100 << endl; // Outputs between 0 and 99
    
    cout << rawRand % 101 << endl; // outputs between 0 and 100
    

    For the people downvoting; note one minute after this was originally posted I left the comment:

    From http://www.cplusplus.com/reference/clibrary/cstdlib/rand "Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans."

    With 64-bit ints and using 100 numbers as output, the numbers 0-16 are represented with 1.00000000000000000455 % of the numbers (an relative accuracy to identically distributed of 1% by about 10-18), while the numbers 17-99 are represented with 0.99999999999999999913 % of the numbers. Yes, not perfectly distributed, but a very good approximation for small spans.

    Also note, where does the OP ask for identically distributed numbers? For all we know these are being used for purposes where a small deviations doesn't matter (e.g., anything other than cryptography -- and if they are using the numbers for cryptography this question is much too naive for them to be writing their own cryptography).

    EDIT - For people who are truly concerned with having a uniform distribution of random numbers the following code works. Note this isn't necessarily optimal as with 64-bit random ints, it will require two calls of rand() once every 10^18 calls.

    unsigned N = 100; // want numbers 0-99
    unsigned long randTruncation = (RAND_MAX / N) * N; 
    // include every number the N times by ensuring rawRand is between 0 and randTruncation - 1 or regenerate.
    unsigned long rawRand = rand();
    
    while (rawRand >= randTruncation) {
        rawRand = rand();  
    // with 64-bit int and range of 0-99 will need to generate two random numbers
    // about 1 in every (2^63)/16 ~ 10^18 times (1 million million times)
    
    // with 32-bit int and range of 0-99 will need to generate two random numbers 
    // once every 46 million times.
    
    }
    cout << rawRand % N << stdl::endl;
    

提交回复
热议问题