Generate random numbers uniformly over an entire range

后端 未结 17 2609
野性不改
野性不改 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 05:00

    Of course, the following code won't give you random numbers but pseudo random number. Use the following code

    #define QUICK_RAND(m,n) m + ( std::rand() % ( (n) - (m) + 1 ) )
    

    For example:

    int myRand = QUICK_RAND(10, 20);
    

    You must call

    srand(time(0));  // Initialize random number generator.
    

    otherwise the numbers won't be near random.

提交回复
热议问题