User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,081 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,968 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1825 | Replies: 3
Reply
Join Date: Nov 2006
Posts: 14
Reputation: elcrapo is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
elcrapo elcrapo is offline Offline
Newbie Poster

Program will not generate random numbers

  #1  
Feb 14th, 2007
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 1:13 pm. Reason: This is C++ code, not PHP code. Tag is [CODE=CPP]
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Program will not generate random numbers

  #2  
Feb 14th, 2007
Originally Posted by elcrapo View 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.

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:
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Nov 2005
Location: Canada
Posts: 234
Reputation: dwks will become famous soon enough dwks will become famous soon enough 
Rep Power: 4
Solved Threads: 21
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Program will not generate random numbers

  #3  
Feb 14th, 2007
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.
   int lowest=1, highest=19;

What exactly do you want?
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote  
Join Date: May 2006
Posts: 2,698
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Program will not generate random numbers

  #4  
Feb 15th, 2007
Originally Posted by dwks View Post
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 2:28 am.
Age is unimportant -- except in cheese
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

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

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:20 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC