Solved

How to generate random FLOAT numbers between a specified range?

Posted on 2003-11-17
3
2,060 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
how to address a class using javah; or, what are fully-qualified java class names? 13 76
thread-safe code in c++ 2 84
How to convert MFC APP to Win32 APP. 19 69
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

911 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

20 Experts available now in Live!

Get 1:1 Help Now