I want to take a random string of 32 bits and declare this bit string to be a float in C++
.
For example the string 0.0111 1111.1000 0000 0000 0000 0000 000
should give me the float corresponding to 1.5
as this is the F32 definition of 1.5 according to IEEE 754-2008. The first 0 mean +ve, the next 8 give you an exponent of 127-127 = 0, and the 23 bit mantissa gives you the .5
.
If a string is not a good data format for this then I'd be happy to hear suggestions.
In fact I want to create a random floating point number in C++ but I don't know how. Where by random I mean each bit is independently 0 or 1. The solutions (for example here: C++ random float number generation) I've found suggest using a random integer generator then dividing by the RAND_MAX
and then multiplying by FLOAT_MAX
or in some other way creating a float according to some uniform distribution. This is no good to me as whilst the values will be evenly distributed, this will generate more floats with large exponents.
Basically this question but for C++: Convert Double to Binary representation?