c++ help(random number between a high and low)

Discussion in 'Programming' started by TfK_Psych, Jul 22, 2007.

  1. #1
    i was wondering if anyone could help me.

    im tacking a c++ course right now and im kinda stuck on one of my labs.

    see the current lab is asking me to use a myrand() function which i was supposed to have made in another lab. it needed the user to enter a high and low value and the random number had to either be between the high and low values or the high and low themselves. i got close but could not get it exactly.
    i have tried random = (low+rand() % (high-low+1)) and random = rand()/low%high as well as a few other pointless ones. all i need is a real basic line of code to make it happen, so i can mave on with my current lab. if anyone can help i would really appreciate it.
     
    TfK_Psych, Jul 22, 2007 IP
  2. TfK_Psych

    TfK_Psych Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    it might help if i included my code : // note: he wanted like twenty random numbers ^_^

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;

    const char space = ' ';

    int myrandom(int low, int high);

    int random;

    int main()
    {
    int high, low;

    cout << "This program will display 9 to 10 random values between a high and low number." << '\n' << '\n';

    cout << "Enter the highest possible value: ";
    cin >> high;

    cout << '\n' << "Enter the lowest possible value: ";
    cin >> low;

    srand(time (0));


    myrandom(low, high);

    cout << '\n' << '\n';

    return 0;
    }

    int myrandom(int low, int high)
    {
    random = rand()/low%high ;

    random += 1;



    if(random < low)

    {

    random = random + rand()%2;

    random += 1;

    random;

    cout << random << space;

    }

    else

    {

    cout << random << space;

    random = rand()/low%high;

    random;

    if(random < low)

    {

    random = random + rand()%2;

    random += 1;

    random;

    cout << random << space;
    }
    else
    {
    cout << random << space;

    }


    }


    random = rand()/low%high;//line 82


    if(random < low)


    {


    random = random + rand()%2;


    random += 1;


    random;


    cout << random << space;




    }


    else


    {


    cout << random << space;


    random = rand()/low%high;


    random;


    if(random < low)


    {



    random = random + rand()%2;



    random += 1;



    random;



    cout << random << space;


    }

    else

    {



    cout << random << space;

    }


    }


    random = rand()/low%high;//line 82


    if(random < low)


    {


    random = random + rand()%2;


    random += 1;


    random;


    cout << random << space;




    }


    else


    {


    cout << random << space;


    random = rand()/low%high;


    random;


    if(random < low)


    {



    random = random + rand()%2;



    random += 1;



    random;



    cout << random << space;


    }

    else

    {



    cout << random << space;

    }


    }


    random = rand()/low%high;//line 82


    if(random < low)


    {


    random = random + rand()%2;


    random += 1;


    random;


    cout << random << space;




    }


    else


    {

    cout << random << space;


    random = rand()/low%high;

    random;


    if(random < low)


    {



    random = random + rand()%2;



    random += 1;



    random;



    cout << random << space;


    }

    else

    {



    cout << random << space;

    }


    }


    random = rand()/low%high;//line 82


    if(random < low)


    {


    random = random + rand()%2;


    random += 1;


    random;


    cout << random << space;




    }


    else


    {


    cout << random << space;


    random = rand()/low%high;


    random;


    if(random < low)


    {



    random = random + rand()%2;



    random += 1;



    random;



    cout << random << space;


    }

    else

    {



    cout << random << space;

    }


    }


    random = rand()/low%high;//line 82


    if(random < low)


    {


    random = random + rand()%2;


    random += 1;


    random;


    cout << random << space;




    }


    else


    {


    cout << random << space;


    random = rand()/low%high;


    random;


    if(random < low)


    {

    random = random + rand()%2;



    random += 1;



    random;



    cout << random << space;


    }

    else

    {



    cout << random << space;

    }


    }



    return random;

    }
     
    TfK_Psych, Jul 22, 2007 IP
  3. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #3
    
    Try low+rand()*(high-low+1)
    
    Code (markup):
     
    it career, Jul 23, 2007 IP
  4. TfK_Psych

    TfK_Psych Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks for the help ^_^
     
    TfK_Psych, Jul 23, 2007 IP
  5. ProgrammersTalk

    ProgrammersTalk Peon

    Messages:
    684
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    LOL!!! That's really a free A..? XD
     
    ProgrammersTalk, Jul 23, 2007 IP