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


Re: Random numbers based on user input 80 80

try this:

rand1 = (rand()%highest)+1;
Re: Random numbers based on user input 80 80

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..

Re: Random numbers based on user input 80 80

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....

Re: Random numbers based on user input 80 80

Number1 % Number2 == Reminder which the reminder is always in the range of 0 to (Number2 - 1)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.