Help with rand please.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 7
Reputation: someguy88 is an unknown quantity at this point 
Solved Threads: 0
someguy88 someguy88 is offline Offline
Newbie Poster

Help with rand please.

 
1
  #1
Oct 7th, 2007
Please help me im having trouble with a dice roller im making.

Its kind of working except i was wondering why it is that every time i start it up it always throws up the same numbers in a row if you put in 100. It probably does that with other numbers to but i haven't checked them.

Anyhow i was wondering if someone could suggest a way to make it actually random, if its possible in C++ and C.

Any suggestions in just imporving it are welcome to THANKS ALL.

  1.  
  2.  
  3.  
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <string>
  7. using namespace std;
  8.  
  9. void loop_q();
  10.  
  11.  
  12. int main()
  13. {
  14.  
  15. int dice;
  16.  
  17. system("cls");
  18.  
  19. cout << "What would you like your dice out of: " << endl;
  20. cout << "(100, 10, 6, 4 ect..)" << endl;
  21. cin >> dice;
  22.  
  23. int rand_number = rand() % dice + 1;
  24.  
  25. system("PAUSE");
  26. system("cls");
  27.  
  28. cout << "Roll: " << rand_number << endl;
  29. cout << endl;
  30. loop_q();
  31. }
  32.  
  33. void loop_q()
  34. {
  35. cout << "Would you like to roll again? y/n." << endl;
  36.  
  37. char answer;
  38. cin >> answer;
  39.  
  40. if (answer=='y')
  41. {
  42. main();
  43. }
  44.  
  45. else
  46. {
  47. return;
  48. }
  49.  
  50. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,413
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1470
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with rand please.

 
0
  #2
Oct 7th, 2007
It generates the same set of numbers every time because you didn't seed it. Do this:
  1. #include <ctime>
  2.  
  3. int main()
  4. {
  5. srand( time(0) ); // see the random number generator
  6.  
  7. // other code here
  8. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 7
Reputation: someguy88 is an unknown quantity at this point 
Solved Threads: 0
someguy88 someguy88 is offline Offline
Newbie Poster

Re: Help with rand please.

 
0
  #3
Oct 7th, 2007
Thanks very much!!

i just have one more question as i wish to learn and am teaching myself from the internet.

It works good now, but i would like to know how it works. If you would care to explain it to me that would be great.

Thanks again mate.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,683
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help with rand please.

 
0
  #4
Oct 7th, 2007
>It works good now, but i would like to know how it works.
These two links should give you more information than you probably want right now.

http://www.eternallyconfuzzled.com/t..._tut_rand.aspx
http://www.eternallyconfuzzled.com/a..._art_rand.aspx
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC