Solved

i need to create a randlomly generated float number

Posted on 2003-11-21
8
348 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 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