944,144 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3688
  • C++ RSS
Feb 14th, 2007
0

Program will not generate random numbers

Expand Post »
I am having troubles getting this program to generate random numbers. Right now i have it set up to only generate 1 and 19. I do not know how to generate random numbers. :eek:

thank you if anyone can help my dumbass.


  1. // Exercise 12.16: MultiplicationTeacher.cpp
  2. // This game asks the user to enter the product of two
  3. // randomly generated numbers in the range 0-9.
  4. #include <iostream> // required to perform C++ stream I/O
  5. #include <ctime> // contains prototype for function time
  6.  
  7. // contains function prototypes for functions srand and rand
  8. #include <cstdlib>
  9.  
  10. using namespace std; // for accessing C++ Standard Library members
  11.  
  12. // function prototypes
  13. int generateQuestion();
  14. void generateOutput();
  15.  
  16. // function main begins program execution
  17. int main()
  18. {
  19. // randomize random number generator using current time
  20. srand( time( 0 ) );
  21.  
  22. // define variables
  23. //int userAnswer = 0; // stores the user's answer
  24. int correctAnswer; // stores the correct answer
  25. int a;
  26. int lowest=1, highest=19;
  27. int range=(highest-lowest)+1;
  28. int input;
  29. int times = lowest * highest;
  30.  
  31. for(int index=0; index<1; index++){
  32.  
  33. a = lowest+int(range*rand()/(RAND_MAX + 1.0));
  34.  
  35. cout << "How much is" << lowest <<""<< "times" <<""<< highest << "(-1 to end)?" << endl;
  36. cin >> input;
  37.  
  38. while(input = times)
  39. cout << "How much is" << lowest <<""<< "times" <<""<< highest << "(-1 to end)?" << endl;
  40. cin >> input;
  41. }
  42.  
  43. return 0; // indicates successful termination
Last edited by WaltP; Feb 14th, 2007 at 2:13 pm. Reason: This is C++ code, not PHP code. Tag is [CODE=CPP]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
elcrapo is offline Offline
14 posts
since Nov 2006
Feb 14th, 2007
0

Re: Program will not generate random numbers

Click to Expand / Collapse  Quote originally posted by elcrapo ...
I am having troubles getting this program to generate random numbers. Right now i have it set up to only generate 1 and 19. I do not know how to generate random numbers.

thank you if anyone can help my dumbass.
change a = lowest+int(range*rand()/(RAND_MAX + 1.0)); to
a = (rand() % highest) + lowest;
This takes the random value, divides it by highest and keeps the remainder (0-18) then adds 1 (0-19)

Now you're a smartass :mrgreen:
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Feb 14th, 2007
1

Re: Program will not generate random numbers

Really? . . . http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx

Either way works, and some would recommend the OP's way over yours (myself included).

If you want it to generate numbers outside of 1-19 or something, change these variables.
C++ Syntax (Toggle Plain Text)
  1. int lowest=1, highest=19;

What exactly do you want?
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Feb 15th, 2007
0

Re: Program will not generate random numbers

Click to Expand / Collapse  Quote originally posted by dwks ...
Really? . . . http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx

Either way works, and some would recommend the OP's way over yours (myself included).
Thanks for the link. Interesting.... I'm convinced

I also noticed an error in my solution. So go ahead and use your original equation. :o
Last edited by WaltP; Feb 15th, 2007 at 3:28 am.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Headers?
Next Thread in C++ Forum Timeline: gotoxy() substitute





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC