Solved

i need to create a randlomly generated float number

Posted on 2003-11-21
8
351 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

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