How do I scale down numbers from rand()?

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

    See man 3 rand -- you need to scale by dividing by RAND_MAX to obtain the range [0, 1] after which you can multiply by 100 for your target range.

提交回复
热议问题