Solved

How to generate random FLOAT numbers between a specified range?

Posted on 2003-11-17
3
2,058 Views
Last Modified: 2007-12-19
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..
0
Comment
Question by:lost_bits1110
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 100 total points
ID: 9765648
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
 

Author Comment

by:lost_bits1110
ID: 9765699
Thaaaaaaaaaaaanx..!
It works..
How'd you figure that out so fast?????!
I'll get there one day.......
:s
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9765739
> How'd you figure that out so fast?????!

Practice!  :)

Dex*
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now