Solved

i need to create a randlomly generated float number

Posted on 2003-11-21
8
346 Views
Last Modified: 2012-08-14
ok.....creating randomly generated nubers is my weakest area in c++.

i need to create a randomly generated number that is from -1.0 and +1.0

i really need the answer to this so the easiest and most quickest answer will get my points
0
Comment
Question by:rholding2000
  • 4
  • 4
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 9799511
Check out http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20800644.html where Dexstar answered a similar Q:

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

and use it like

float fRand = GetRandomRange ( -1.0, 1.0);
0
 

Author Comment

by:rholding2000
ID: 9799673
ok i want to generate 3 of these numbers, to put into an opengl "gltranslatef(float1, float2, float3)" function how do i do this as the output of the random number to the screen is the same all the time
 
0
 
LVL 86

Expert Comment

by:jkr
ID: 9799693
Didn't you 'srand()'? OK, in this case, you'd


#include <time.h>

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

float f1, f2, f3;

f1 = GetRandomRange ( -1.0, 1.0);
f2 = GetRandomRange ( -1.0, 1.0);
f3 = GetRandomRange ( -1.0, 1.0);

0
 

Author Comment

by:rholding2000
ID: 9799887
if i output this to the screen

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


int main(int argc, char** argv)  // ignore this its opengl
{


float f1, f2, f3;

f1 = GetRandomRange ( -1.0, 1.0);
f2 = GetRandomRange ( -1.0, 1.0);
f3 = GetRandomRange ( -1.0, 1.0);

cout << f1 << endl;
cout << f2 << endl;
cout << f3 << endl;

the 3 numbers are the same.......is there a way to get these 3 different
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

by:jkr
ID: 9800009
Hm, keep in mind that 'rand()' only creates pseudo-random numbers. Try

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


int main(int argc, char** argv)  // ignore this its opengl
{


float f1, f2, f3;


srand( (unsigned)time( NULL ) );

f1 = GetRandomRange ( -1.0, 1.0);
f2 = GetRandomRange ( -1.0, 1.0);
f3 = GetRandomRange ( -1.0, 1.0);

cout << f1 << endl;
cout << f2 << endl;
cout << f3 << endl;

return 0;
}
0
 

Author Comment

by:rholding2000
ID: 9800028
ok ive done this can someone help me get it to only produce numbers from -1 to 1....man this is killing me...i thank jkr for getting me so close to the solution....sorry for my idiocy in this langauge :/

float random::gNum(float maxNum, float seedMod)
{
    srand(seedMod + (unsigned)time( NULL ) );
      for (int count = 0; count < 1; count++)
      {
                     rand();
      }
      float output = (float)(maxNum * rand() / (RAND_MAX + 1.0));
      return output;
}

void main()
{
            random testrandom;
            for (int count = 0; count != 10; count++)
            {
            float test = testrandom.gNum(12, count) - 2 ;
            std::cout << test << std::endl;
            }
0
 
LVL 86

Expert Comment

by:jkr
ID: 9800052
Hm, what I posted above works just fine for me:

C:\tmp\cc>randfl
0.444502
0.721366
-0.785943

0
 

Author Comment

by:rholding2000
ID: 9800122
hmmmmmm mines 0.337..... if i recompile it goes 0.338, 339.........anyways thanks for the help man i really appreciate it so ill give the the points cos youve helped me to a point where im geting the hang of it now

thanks a lot dude......rik..
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

706 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

18 Experts available now in Live!

Get 1:1 Help Now