Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to generate random FLOAT numbers between a specified range?

Posted on 2003-11-17
3
Medium Priority
?
2,074 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

704 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