Generate random numbers uniformly over an entire range

后端 未结 17 2722
野性不改
野性不改 2020-11-22 04:21

I need to generate random numbers within a specified interval, [max;min].

Also, the random numbers should be uniformly distributed over the interval, not located to

17条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 04:59

    If RAND_MAX is 32767, you can double the number of bits easily.

    int BigRand()
    {
        assert(INT_MAX/(RAND_MAX+1) > RAND_MAX);
        return rand() * (RAND_MAX+1) + rand();
    }
    

提交回复
热议问题