Solved

How to generate random FLOAT numbers between a specified range?

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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 …
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…
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.

726 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