How to implement a random float function so that it does not lose entropy? (PHP)

ojackgk

New Member
I am trying to generate random floats using nothing but bytes I get from /dev/urandom. Currently my best idea is to get the platform precision doing something like:\[code\]$maximumPrecision = strlen('' . 1/3) - 2;\[/code\]and then construct a string of 0-9 in a loop the number of times $maximumPrecision tells us. For examle, if the precision is 12, I would generate 12 random numbers and concatenate them. I think it's an ugly idea.Update: Does this make sense?\[code\]$bytes =getRandomBytes(7); // Just a function that returns random bytes.$bytes[6] = $bytes[6] & chr(15); // Get rid off the other half$bytes .= chr(0); // Add a null byte$parts = unpack('V2', $bytes);$number = $parts[1] + pow(2.0, 32) * $parts[2];$number /= pow(2.0, 52);\[/code\]
 
Back
Top