Generate uniformly distributed random number between 0 and 1 based on unsigned int

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have at my disposal a 32-bit unsigned int produced by a uniform random number generator routine. I would like to convert this number to a float in the range [0,1]. Obviously this is most easily done by dividing the unsigned int by UINT_MAX, casting both to float before division. Unless I m very mistaken, I think such a division will cost me a lot of precision and is therefore not ideal.

Since this action will be performed in openCL, I assume not to have double variables I could use intermediately.

Any ideas how I could generate the float in a better way?

Thanks in advance!

Tom

Answers

I don t think so.

You only have 32 bits of precision available to you anyway, so casting to a (presumably 32-bit) float and back should be OK.

You will have the 32-bit quantization "error", but that s based on the original 32-bit data type, not the float.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/12586797/generate-uniformly-distributed-random-number-between-0-and-1-based-on-unsigned-i

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils