Use rand() to generate uniformly distributed floating point numbers on (a,b), [a,b), (a,b], and [a,b]

后端 未结 5 1357
失恋的感觉
失恋的感觉 2021-01-06 06:27

I want to collect the \"best\" way to generate random numbers on all four types of intervals in one place. I\'m sick of Googling this. Search results turn up a lot of crap.

5条回答
  •  醉酒成梦
    2021-01-06 07:02

    First, generate random numbers on [a,b]. To generate random numbers on [a,b), just generate a random number on [a,b], check if it equals b, and if so try again. Similarly for all the other open interval variants.

提交回复
热议问题