[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

i need to create a randlomly generated float number

Posted on 2003-11-21
8
Medium Priority
?
357 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 600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.

873 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