Thanks to visit codestin.com
Credit goes to github.com

Skip to content

specbranch/fp-rand

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Perfect Floating Point Random Number Generator

Algorithm and go reference implementation for drawing random numbers between 0 and 1 in a way that is floating-point aware. This is the first efficient algorithm that can access the entire range of floaitng-point numbers between 0 and 1, and is distributed exactly as expected if you were to draw a random real number from (0, 1) and then round to floating point in any rounding mode.

The "round down" rounding mode is a strict upgrade from most current floating-point random number generation routines. It generates an output strictly in [0, 1) and can access any floating-point number in that range, including the subnormal numbers.

The "round to nearest" and "round up" routines provide slightly different distributions that can return 1, but they are distributed as though the real number were rounded from that range. This subtly affects the range generated, but brings random number generation in line with every other floating-point function.

Brief Algorithm Description

Current floating-point random number generators tend to generate a number with p bits, where p is the precision of the target floating-point format, then divide by (1 << p). This leaves gaps between the possible outputs and makes a large number of possible outputs unreachable.

The algorithm here has two important changes:

  1. We backfill bits into the tail of the random number depending on the output magnitude.
  2. If the output is zero, we regenerate a new random number in a compressed range, allowing lower-probability numbers to be reached.

(1) here allows us to fill the gaps between the numbers that would be generated, and (2) allows us to access every order of magnitude down to zero.

A detailed description of why this works is in the pdf paper here.

About

Floating-point random number generation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published