Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Templated Random Number Generator (http://www.programmingforums.org/showthread.php?t=13482)

Ben.Dougall Jul 5th, 2007 7:03 PM

Templated Random Number Generator
 
Hi guys, new to the forum, plan to contribute here a lot as i get bored at work. Quick question on a Random Generator.

I'm making a library for common tasks in C++ ( random, XML Reader, HTML Document maker, etc ) and I am just wondering the best way to go about this is. Right now, even though it is templated, doubles and floats are never real numbers, always whole numbers.

:

  1.         /**
  2.         @brief        Seeds the Random Number generator
  3.         @param        seed        The value to seed the generator with.
  4.         @note        The generator is only seeded once, no matter how many times you call the method */
  5.         void SeedGenerator( int seed = static_cast<int>( time(0) ) )
  6.         {
  7.                 static bool seeded = false;
  8.                 if ( !seeded )
  9.                 {
  10.                         srand( seed );
  11.                         rand();
  12.                         seeded = true;
  13.                 }
  14.         }
  15.  
  16.         /**
  17.         @brief        Generates Random Number
  18.         @param        low                The lowest possible number
  19.         @param        high        The highest possible number
  20.         @return        The random number between the range.
  21.         */
  22.         template <typename T>
  23.         inline T RandomNumber( const T &low, const T &high )
  24.         {
  25.                 return static_cast<T>(low + rand() % high );
  26.         }


I have been using this formula for 4 years during Java development, and works fine for whole number generation. I was just wondering what the best, and most efficent method of doing this would be.

lectricpharaoh Jul 6th, 2007 1:41 AM

Quote:

Originally Posted by Ben.Dougall
Hi guys, new to the forum, plan to contribute here a lot as i get bored at work. Quick question on a Random Generator.

That's as good a reason as any, I suppose. Let me be the first to welcome you to the forums. :)
Quote:

Originally Posted by Ben.Dougall
I have been using this formula for 4 years during Java development, and works fine for whole number generation. I was just wondering what the best, and most efficent method of doing this would be.

Narue has an excellent article here on using rand(), particularly how to use it effectively, avoiding common pitfalls that often yield sequences that are 'less random'. She also has another article that shows you how to roll your own psuedo-random number generator. This might be necessary if the implementation in your compiler's libraries is poor, or simply because you want to have multiple concurrent generators active. As an example of why you might need multiple generators, imagine a Tetris clone that allowed two-player head-to-head games. By using different generators seeded with the same value, you guarantee neither player gets an advantage by having better pieces (since they both get the same pieces in the same order).

Ben.Dougall Jul 6th, 2007 10:13 AM

Thanks for the info, those articles are quite useful. I'll give it a try when I get some time.

Thanks again.

Ben.Dougall Jul 6th, 2007 7:09 PM

:

  1. static_cast<T>(low + rand() / ( RAND_MAX / ( high - low ) + 1 ));


Thank you Lectric, the first link you gave me seems to have done the job.

DaWei Jul 6th, 2007 7:19 PM

You understand that the return of rand, as usually implemeted in C/C++, is an integer, right? Casting doesn't give you a real number.

Ben.Dougall Jul 6th, 2007 7:42 PM

Quote:

Originally Posted by DaWei (Post 130172)
You understand that the return of rand, as usually implemeted in C/C++, is an integer, right? Casting doesn't give you a real number.

True, I had that in to eliminate a warning I got with a former formula. One of my professors docked 30% if a compile warning occured. Removed, and no warning. Thanks for your eyes on that one.

I have a feeling i can learn a lot from this forum. Thank again Dawei and lectric.


All times are GMT -5. The time now is 3:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC