-
C++ (
http://www.daniweb.com/forums/forum8.html)
| elcrapo | Feb 14th, 2007 1:14 pm | |
| Program will not generate random numbers 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. :confused::?::twisted::eek:
thank you if anyone can help my dumbass.
// Exercise 12.16: MultiplicationTeacher.cpp
// This game asks the user to enter the product of two
// randomly generated numbers in the range 0-9.
#include <iostream> // required to perform C++ stream I/O
#include <ctime> // contains prototype for function time
// contains function prototypes for functions srand and rand
#include <cstdlib>
using namespace std; // for accessing C++ Standard Library members
// function prototypes
int generateQuestion();
void generateOutput();
// function main begins program execution
int main()
{
// randomize random number generator using current time
srand( time( 0 ) );
// define variables
//int userAnswer = 0; // stores the user's answer
int correctAnswer; // stores the correct answer
int a;
int lowest=1, highest=19;
int range=(highest-lowest)+1;
int input;
int times = lowest * highest;
for(int index=0; index<1; index++){
a = lowest+int(range*rand()/(RAND_MAX + 1.0));
cout << "How much is" << lowest <<""<< "times" <<""<< highest << "(-1 to end)?" << endl;
cin >> input;
while(input = times)
cout << "How much is" << lowest <<""<< "times" <<""<< highest << "(-1 to end)?" << endl;
cin >> input;
}
return 0; // indicates successful termination |
| WaltP | Feb 14th, 2007 2:11 pm | |
| Re: Program will not generate random numbers Quote: Originally Posted by elcrapo (Post 315275) 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: |
| dwks | Feb 14th, 2007 4:59 pm | |
| 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.
int lowest=1, highest=19;
What exactly do you want? |
| WaltP | Feb 15th, 2007 3:28 am | |
| Re: Program will not generate random numbers Quote: Originally Posted by dwks (Post 315366) | 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 |
| All times are GMT -4. The time now is 9:50 pm. | |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC