and sorry if this is a duplicate of some sort, my poor technical vocabulary doesn't get me very far with google.

I am trying to implement some well-known PRNG algorithms. For the time, the algorithm in themselves do their job very well (generating 16/32/64/128 random bits). I am now trying to create a uniform distribution function, that, taking as input a 16/32/64/128 bit integer spits a IEEE compliant binary2, binary4 or binary8 (the function doesn't have to handle all of them at once, but by able to somewhat template for the other floating point types.

As quoted in the title of the question, the output shall be between -1.0 and 1.0 . A naive implementaion would be to take a byte of the integers, and affect it to the exponent, while leaving the rest to the mantissa (padding with 0s if there's not enough bits), but how to handle denormals (the code is performance critical, and the target is reluctant with un-uniform data-based condition). How to handle these then ?.

And while that may be asking two questions at once, how to do the same for a normal distribution ?

Any help or guidance is appreciated. Thanks in advance


You can avoid getting denormals by truncating the number of random bits to ensure that there are none; even if you don't do that, the probability of getting a denormal is extremely small.

The standard way to convert uniform random variables to Gaussians is the Box–Muller transform.

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.