| | |
Program will not generate random numbers
![]() |
•
•
Join Date: Nov 2006
Posts: 14
Reputation:
Solved Threads: 0
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.


:eek:thank you if anyone can help my dumbass.
c Syntax (Toggle Plain Text)
// 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
Last edited by WaltP; Feb 14th, 2007 at 2:13 pm. Reason: This is C++ code, not PHP code. Tag is [CODE=CPP]
•
•
•
•
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.
a = lowest+int(range*rand()/(RAND_MAX + 1.0)); toa = (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:
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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.
What exactly do you want?
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)
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
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
•
•
•
•
Really? . . . http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
Either way works, and some would recommend the OP's way over yours (myself included).
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- Generating random numbers using VC6 (C++)
- need help, need to generate 10 random numbers, such that their sum is less than 1 (C)
- generating random numbers into an array.. (Java)
- Trying to find a better way to generate random numbers in Dev Pascal (Pascal and Delphi)
- generate random question (C)
- random numbers all different??? (C++)
Other Threads in the C++ Forum
- Previous Thread: Headers?
- Next Thread: gotoxy() substitute
| Thread Tools | Search this Thread |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix matrix3d memory multiple news node output parameter pointer problem program programming project python random read recursion reference rpg std::coutwstring string strings temperature template test text text-file tree url variable vector video visualization win32 windows winsock word wordfrequency wxwidgets






