Solved

i need to create a randlomly generated float number

Posted on 2003-11-21
8
349 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 clear a vector as well as how to detect empty vectors in C++.

821 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