How to generate random FLOAT numbers between a specified range?

Hello experts..!

How can I generate random float numbers in a particular range?

I know I can use: (float)rand()

But how could I get numbers between the ranges -5 and 20 lets say?

Thanks..
lost_bits1110Asked:
Who is Participating?
 
DexstarCommented:
lost_bits1110:

> I know I can use: (float)rand()

> But how could I get numbers between the ranges -5 and 20 lets say?

float GetRandomRange( float nFirst, float nSecond )
{
     return nFirst + (nSecond-nFirst)*( ((float)rand()) / ((float)RAND_MAX) );
}

Hope That Helps,
Dex*
0
 
lost_bits1110Author Commented:
Thaaaaaaaaaaaanx..!
It works..
How'd you figure that out so fast?????!
I'll get there one day.......
:s
0
 
DexstarCommented:
> How'd you figure that out so fast?????!

Practice!  :)

Dex*
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.