Generating random integer from a range

前端 未结 13 1512
失恋的感觉
失恋的感觉 2020-11-22 03:12

I need a function which would generate a random integer in given range (including border values). I don\'t unreasonable quality/randomness requirements, I have four requirem

13条回答
  •  心在旅途
    2020-11-22 04:02

    The following expression should be unbiased if I am not mistaken:

    std::floor( ( max - min + 1.0 ) * rand() ) + min;
    

    I am assuming here that rand() gives you a random value in the range between 0.0 and 1.0 NOT including 1.0 and that max and min are integers with the condition that min < max.

提交回复
热议问题