-1

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?

Community
  • 1
  • 1
R.Mckemey
  • 335
  • 1
  • 3
  • 14
  • 1
    If all you want is a random floating point number see: http://stackoverflow.com/questions/686353/c-random-float-number-generation – NathanOliver Aug 23 '16 at 16:36
  • 2
    `int32_t x = /* random value */; float f = *(float *) &x;`. Not saying this is a good idea or won't break any rules. But it can be done. – Some programmer dude Aug 23 '16 at 16:36
  • std::bitset, maybe? Also, it seems like you're asking two completely different questions. Could you clarify? –  Aug 23 '16 at 16:37
  • You need to explain more about that format than simply declaring that that string represents 1.5, since it's not at all obvious what the connection is. – Pete Becker Aug 23 '16 at 16:37
  • 3
    If you want to generate a random floating point value, look at e.g. [this reference of the standard pseudo-random generation facilities in C++](http://en.cppreference.com/w/cpp/numeric/random). – Some programmer dude Aug 23 '16 at 16:38
  • Thanks @JoachimPileborg your first answer was exactly what I was looking for. From reading the documentation around random floating point numbers most of them seem to generate numbers with randomly distributed values, I need each bit to randomly be 0 or 1. This is a very different condition. – R.Mckemey Aug 23 '16 at 16:57
  • @JETM if std::bitset had a to_float function it would have been perfect but I can't see one. Thanks tho. – R.Mckemey Aug 23 '16 at 16:59
  • Do note that my first "solution" breaks the strict aliasing rule. – Some programmer dude Aug 23 '16 at 17:05

1 Answers1

1

If you just want a RNG that spits out a exponentially distributed floats, you don't have to worry about binary representation. Just use math.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <math.h>

int main (void) {
    int i;

    srand (time(NULL));

    // Make 10 'random' floats
    for (i = 0; i < 10; ++i)
    {
        // Float exponents go from -128 to 127
        float exponent = -128 + 256 * (float) rand() / RAND_MAX;

        float result = pow(2, exponent);

        printf ("%g\n", result);
    }

    return 0;
}

If you want to arbitrarily generate bytes interpreted as float, you can do this instead...

int i = rand();
float result = *((float *)&i);

printf ("%g\n", result);
QuestionC
  • 10,006
  • 4
  • 26
  • 44
  • I worry that there might be problems with this that would mean it wouldn't be fully random. For example if you square a number in decimal it's final digit cannot ever be a 2, 3 or 7 (as the lowest digit of the square is entirely determined by the lowest digit of the input). I know this doesn't apply here there might be something similar. In particular on my box `rand()` gives a 31 bit integer so there are at most 2^31 outputs from this function compared to the 2^32 different F32 numbers (albeit 2^23 of them represent NaN). – R.Mckemey Aug 24 '16 at 09:22
  • 1
    I updated my answer with a code snippet that just takes the bytes of an `int` and shove it into a `float`. How's that? – QuestionC Aug 24 '16 at 14:23