Hi guys

Im trying to make a maths game for a friend of mines little brother jase, but I need it to generate 2 random numbers between 1 and the maximum number that jase said. I cant figure out how to get the rand1 = (rand()%10)+1; so that the 10 = whatever number he entered.
here is the code i have so far before i became stuck

#include <ctime>
#include <cstdlib>

using namespace std;

int main()
    int choice
    cout << "Welcome to jase's maths game, what operations would you like to practice\n";
    cout << "1) Addition +\n";
    cout << "2) Subtraction -\n";
    cout << "Please enter your choice 1 or 2 then press enter: \n";
    cin >> choice << endl;// Find out what jase wants to do

    if (choice == 1)//If jase wants to do addition
        int lowest=1
        int highest
        int rand1
        int rand2
        cout << "What is the biggest number you want to use: ";
        cin >> highest << endl;
        rand1 = (rand()%10)+1;// I need 10 to be the maximum number jase entered

Can somebody please show me how to do this i would be very grateful?

Many thanks


10 Years
Discussion Span
Last Post by invisal

gabs is right....it's simple..u can generate in any range....
what happenes is that rand() generates random number irrespective of this %n....anything can be used in place of n....

what we do is just take mod with n so that we can confine our randome number in range 0 to n....that's all..


one more thing....this will generate same randome number for same value of "highest" entered by jase...

if u want to make it more randome use seeded randome number generator...

void srand(int seed);
call this function before rand()%n....with different value of seed it will prompt rand() to generate different random numbers...

to get rid of sending this seed...u can use system clock as seed by calling..


time(0) will send current time as seed to srand...as time will keep changing rand will generate different numbers....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.