Rand between numbers with comma

2

How to generate a number float between two numbers?

Eg: A random number between -270.33 and -47.5.

    
asked by anonymous 13.05.2016 / 23:49

4 answers

4

First of all, @bigown's answer already gives the solution that I understand to be the most technically correct for the problem, especially considering that the question asks for a solution in C ++.

I have posted this answer only as a complement to the comment left in the question, specifically on rand , since it is perfectly possible to use rand() to obtain the floating point result using basic math if the range between the minimum and the maximum is not too long:

res = min + rand() * (max - min) / RAND_MAX;

See working at IDEONE .

Considering that rand() returns a value between 0 and RAND_MAX , we use the division by this constant so that the result strip is homogeneously distributed, which is not possible with the remainder / modulo operator ( % ), which is more likely to return smaller numbers, as used in many examples "internet out".

This solution has no problem with rand using integers, as long as the range between minimum and maximum is proportionally compatible with RAND_MAX in relation to the desired decimal places.

    
14.05.2016 / 00:38
4

Just use the standard library functions available for random number generation:

random_device seed;
mt19937 gen(seed());
uniform_real_distribution<float> dis(-270.33, -47.5);
cout << dis(seed);

See working on ideone and on CodingGround .

Documentation .

    
14.05.2016 / 00:28
1

If you want to use rand , not one of the functions that support floats (like the bigown answer), you will need to convert the result from rand to float (preferably between 0 and 1) before extend the value to the range you want.

You can do this by using a bit of bit manipulation so that the floating-point representation of the value is what you want, as shown in the code below (or running on Ideone )

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    for (int i = 0; i < 10; i++) {
        int r = rand();
        // Assumindo que RAND_MAX seja pelo menos 2^32-1
        //printf("r = %d\n");

        // zera bit de sinal, expoente
        r = (r & 0x3FFFFFFF) | 0x3F800000; // zera bits de sinal de 
        //printf("r = %d\n");

        // gera numero entre 1 e 2
        float f = *((float *)&r);
        //printf("f = %f\n", f);

        // normaliza entre 0 e 1
        f = f - 1;
        //printf("f = %f\n", f);

        float min = -270.33f;
        float max = -47.5f;

        float fRand = min + (f * (max - min));
        printf("fRand = %f\n", fRand);
    }

    return 0;
}
    
14.05.2016 / 00:33
0

The following program illustrates how to sort double values using the rand() function:

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

#define MIN    (-270.33f)
#define MAX    (-47.5f)


double randf( double min, double max )
{
    return min + (rand() / ( RAND_MAX / ( max - min) ) ) ;  
}


int main( int argc, char * argv[] )
{
    int i = 0;

    srand(time(NULL));

    for( i = 0; i < 10; i++)
        printf("%g ", randf( MIN, MAX ) );

    return 0;
}

/* fim-de-arquivo */
    
14.05.2016 / 01:23