Looping Code for Random Number Generator

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>
using namespace std;

int main ()
{
  long double prime1(258949), prime2(73127);
  unsigned int 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.
Last edited on
well, this is really easy to find the answer. didn't you search this site/google??
http://www.cplusplus.com/reference/cstdlib/rand/

you could try a while (true) loop, and add a break when you want the program to finish...
Do you mean something like this?:

1
2
3
4
5
6
7
8
9
10
11
12
13
#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.
This guy explains greatly how you can easily use a random number generator, highly recommend you watching it if you are looking for an easy method: http://www.thenewboston.org/watch.php?cat=16&number=27

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(unsigned short 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.
Last edited on
@Minimacfox
Not press enter several times, but be able to generate multiple random numbers with successive key presses.
Eg.,

"Press ENTER to generate a random number!"
*ENTER*
"The random number is x"
*ENTER*
"The random number is y"
etc.
If I understand it correctly it's the code I show in the 'Fix' part.
Topic archived. No new replies allowed.