Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 5th, 2007, 7:03 PM   #1
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
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.

cpp Syntax (Toggle Plain Text)
  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.

Last edited by Ben.Dougall; Jul 5th, 2007 at 7:39 PM.
Ben.Dougall is offline   Reply With Quote
Old Jul 6th, 2007, 1:41 AM   #2
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 925
Rep Power: 4 lectricpharaoh will become famous soon enough
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).
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old Jul 6th, 2007, 10:13 AM   #3
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
Thanks for the info, those articles are quite useful. I'll give it a try when I get some time.

Thanks again.
Ben.Dougall is offline   Reply With Quote
Old Jul 6th, 2007, 7:09 PM   #4
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
cpp Syntax (Toggle Plain Text)
  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.
Ben.Dougall is offline   Reply With Quote
Old Jul 6th, 2007, 7:19 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 6th, 2007, 7:42 PM   #6
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
Quote:
Originally Posted by DaWei View Post
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.
Ben.Dougall is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[vb6] need advice concerning a largescale non-recurring random number generator chepfaust Visual Basic 8 Jun 8th, 2006 5:04 AM
Median/Mode in arrays? {Need help} Java|Tera Java 27 Nov 29th, 2005 10:50 AM
FiveDigit + RandomeNumber Game. TecBrain Java 0 Nov 18th, 2005 2:53 PM
Random Number & Average Problem Hadrurus Java 6 Aug 15th, 2005 1:08 PM
non repeating random number generation gencor45 C# 2 Feb 9th, 2005 12:11 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:29 PM.

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