How do I scale down numbers from rand()?

后端 未结 9 1948
小蘑菇
小蘑菇 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:03

    If you are using C++ and are concerned about good distribution you can use TR1 C++11 .

    #include 
    
    std::random_device rseed;
    std::mt19937 rgen(rseed()); // mersenne_twister
    std::uniform_int_distribution idist(0,100); // [0,100]
    
    std::cout << idist(rgen) << std::endl;
    

提交回复
热议问题