Generating random numbers on open-open interval (0,1) efficiently

后端 未结 4 849
有刺的猬
有刺的猬 2021-01-05 21:08

I\'m looking for an efficient way to generate random floating-point numbers on the open-open interval (0,1). I currently have an RNG that generates random integers on the cl

4条回答
  •  情歌与酒
    2021-01-05 21:36

    I'm looking for an efficient way to generate random floating-point numbers on the open-open interval (0,1). I currently have an RNG that generates random integers on the closed-closed interval of [0, (2^32)-1]. I've already created a half-open floating point RNG on the interval [0,1) by simply multiplying my result from the integer RNG by 1/((2^32)-1)

    This means that your generator 'tries' to produce 2^32 different values. Problem is, float type is 4 bytes long, thus having less than 2^32 distinct defined values overall. To be precise, there can be only 2^23 values on interval [1/2, 1). Depending on what you need it may be a problem or not.

    You may want to use lagged Fibonacci generator (wiki) with iteration iteration formula from russian wiki
    This already produces numbers from [0,1), given that initial values belong to that interval and may be good enough for your purposes.

提交回复
热议问题