• Register
1 vote
36 views

Problem:

How do I generate random float numbers between 0 to 1 in C++?

12 7 7
15,250 points

1 Answer

0 votes

Solution:

Here you go

float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);

You may use the rand() function 

double randMToN(double M, double N)
{
    return M + (rand() / ( RAND_MAX / (N-M) ) ) ;  
}

This will generate a random number in the range from [M,N) (Considering M=0 and N=1).

 

13 9 6
94,240 points

Related questions

0 votes
1 answer 3 views
3 views
Problem: I have started learning programming in C# recently. I want my program to generate a random number between 0 and 1 and assign it to a variable. How can I do that?
asked Nov 3 Sheeza 3.5k points
0 votes
1 answer 7 views
0 votes
1 answer 13 views
13 views
Hello, I'm looking for a random function in C / C ++: the random () function of C ++ returns a value between 0 and X but I would need a function like: random (4,5,7,9) which would return either 4 or 5 or 7 or 9. I don't know if it exists and that's why I'm asking. thank you
asked Aug 6 amna 36.8k points
1 vote
1 answer 20 views
20 views
Problem: Well, Java is a fun programming language to learn. I am adopting this language very quickly, I guess. I am wondering if I can generate a random double between 0 and 1 in java. Is it possible? Could you guys shed some light on this? Many Thanks!
asked Apr 6 Gavin 15.3k points
0 votes
1 answer 19 views
19 views
I am new to this. I just want to know how to do that?
asked Nov 5 TeamScript 1.8k points