Generating random integer from a range

前端 未结 13 1500
失恋的感觉
失恋的感觉 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:03

    A fast, somewhat better than yours, but still not properly uniform distributed solution is

    output = min + (rand() % static_cast<int>(max - min + 1))
    

    Except when the size of the range is a power of 2, this method produces biased non-uniform distributed numbers regardless the quality of rand(). For a comprehensive test of the quality of this method, please read this.

    0 讨论(0)
提交回复
热议问题