3
How to generate a number float
between two numbers?
Ex.: A random number between -270.33 and -47.5.
3
How to generate a number float
between two numbers?
Ex.: A random number between -270.33 and -47.5.
7
First of all, @Maniero’s answer already gives the solution that I understand to be the most technically correct for the problem, even more considering that the question asks solution in C++.
I posted this reply only as a supplement to the comment left in the question, specifically on the rand
, because it is perfectly possible to use the rand()
to obtain the result with floating point, using basic mathematics, if the range between minimum and maximum is not too extensive:
res = min + rand() * (max - min) / RAND_MAX;
See working on IDEONE.
whereas rand()
returns a value between 0
and RAND_MAX
, we use the division by this constant so that the range of results is distributed homogeneously, which is not possible with the rest/module operator ( %
), that is more likely to return smaller numbers, as used in many examples "internet away".
This solution has no problem with the fact of rand
use integers, provided that the range between minimum and maximum is proportionally compatible with the RAND_MAX
to the desired decimal places.
6
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);
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
1
If you want to use rand
, and not one of the functions they support floats (as the response of Maniero), you will need to convert the result of rand
in a float
(preferably between 0 and 1) before extending the value to the crease that 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;
}
0
The following program illustrates how to draw values double
using the function rand()
:
#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 */
Browser other questions tagged c++ random
You are not signed in. Login or sign up in order to post.
Rand() works with integer numbers... And I don’t think this type of comment helps to solve my problem.
– Jonathan Barcela
What ever tried, and what the difficulty?
– Bacco