samedi 1 mai 2021

How to get random numbers from a range?

I want to be able to have the loop print different random numbers each time I press the number that corresponds to the range I want. However, with what I have below, what happens is that.

I press 2 ----> gives me $16 and subtracts 10 mins.

I press 2 again ----> gives me $16 again and subtracts 10 mins from my initialTime.

What I want for it to do is that even if I press 2 again, it gives me another random number from the range I gave. Below is my code.

#include <cstdlib>
#include <ctime>  
#include <iostream>

int main(int argc, char** argv) 
{
    srand((unsigned)time(0));
    int groupNumber1,initialMoney1, initialTime1, teensMoney, familyMoney, teensTime, familyTime;
    initialMoney1 = 0;
    initialTime1 = 30;
    teensMoney = 1 + rand() % ((5 + 1) - 1);
    familyMoney = 4 + rand() % ((25 + 1) - 4);
    teensTime =  2 + rand() % ((4 + 1) -2);
    familyTime =  6 + rand() % ((12 + 1) - 6);

for (int i=0; i<5; i++)
{
    system("CLS");
    cout<<name1<<", choose what group to serve. \n" "Choose wisely. \n"
        "1 - A group of teenagers \n" 
        "2 - A family \n"

    cout<<'\n';
    cout<<"Your choice: ";
    cin>>groupNumber1;
    cout<<'\n';
    cout<<"You chose group number "<<groupNumber1<<"."<<endl;
    cout<<'\n';

        if ((groupNumber1 == 1))
            {
                cout<<"You now have a total of "<<"$"<< (initialMoney1 += teensMoney)<<" and have "<< ( initialTime1 -= teensTime) <<" minutes remaining."<<endl;   
            }
        else if ((groupNumber1 == 2))
            {
                cout<<"You now have a total of "<<"$"<< (initialMoney1 += familyMoney) <<" and have "<< (initialTime1 -= familyTime) <<" minutes remaining."<<endl;  
            }           



Aucun commentaire:

Enregistrer un commentaire