I am making a random number generator an I want to loop the code so that every time the user hits enter, the formula is applied to the current value for "randomnumber" and the new value is displayed. How do I do it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//generates random numbers
#include <iostream>
#include <windows.h>
#include <string>
#include <limits>
usingnamespace std;
int main ()
{
longdouble prime1(258949), prime2(73127);
unsignedint randomnumber;
cout << "Press ENTER to generate a random number!\n";
randomnumber = //formula will go here
}
Yes, areas of ^that code^ are woeful, but this is more for general interest and curiosity than professionalism.
#include <iostream>
#include <ctime>
main(){
long numbers[] = {258949, 73127};
cout << "Press ENTER to generate a random number!\n";
cin.ignore();
srand(time(0));
int randomNumber = rand()%2;
cout << "The random number generated is " << numbers[randomNumber] << endl;
}
Actually, I wanted to use my own formula for the generation of the number (thats why I declared those two primes).
I just needed a method to loop it so that the user could hit enter more than once.
I just needed a method to loop it so that the user could hit enter more than once.
Like this?
1 2 3 4 5 6 7 8 9
long numbers[] = {258949, 73127};
cout << "Press ENTER to generate a random number!\n";
for(int i=0; i<3; i++)
cin.ignore();
srand(time(0));
int randomNumber = rand()%2;
cout << "The random number generated is " << numbers[randomNumber] << endl;
I'm not quite sure what you are looking for, why would you want him to press enter several times?
EDIT: OHHH, you want to loop the program over and over? And on the same time creating a random number each time?
Fix:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void timer(unsignedshort i){
clock_t delay = i*CLOCKS_PER_SEC;
clock_t start = clock();
while(clock() - start < delay);
}
main(){
srand(time(0));
for(; ;){
cout << "Press ENTER to generate a random number!\n"; cin.ignore();
int randomNumber = 1+(rand()%100);
cout << "The random number generated is " << randomNumber << endl;
timer(3);
system("cls");
}
}
Don't mind the 'timer' function, it's just so you have time to see the random generated number.