Help!!!!!!!!!!!!!!

This is a discussion on Help!!!!!!!!!!!!!! within the C++ Programming forums, part of the General Programming Boards category; *crying* is it possible to make a max number for the rand comand its going to high!!!!! *crying*...

  1. #1
    Registered User mill1000's Avatar
    Join Date
    Nov 2001
    Posts
    45

    Unhappy Help!!!!!!!!!!!!!!

    *crying* is it possible to make a max number for the rand comand its going to high!!!!! *crying*

  2. #2
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    yes if you do this:

    Code:
    rand() % 10;
    it will go from 0 to 10

    if you do this:

    Code:
    (rand() % 10) + 3;
    it will go from 3 to 10
    the best things in life are simple.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    well....almost:

    rand() % 10 will return numbers 0 - 9 inclusive;

    and (rand % 8) + 3 will return return numbers 3 - 10 inclusive;

  4. #4
    Registered User mill1000's Avatar
    Join Date
    Nov 2001
    Posts
    45

    Question

    this is kinda stupid but wat does inclusive mean
    and it din't work
    Last edited by mill1000; 04-05-2002 at 10:22 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    15
    inclusive = numbers from 0 to 9 and including 0 and 9.

    Why doesn't it work? Does it compile?

    Did you include stdlib? I think the rand function is in ther

  6. #6
    Registered User mill1000's Avatar
    Join Date
    Nov 2001
    Posts
    45

    Exclamation

    it din't minimize the rand cout i wanted it between 140 - 160 but it still went higher no as high but it went to about 700

  7. #7
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Well, lets see the way you have it written.

    But lets simplify it first:

    Try making a for loop that sends a random number between 140 and 170 to cout, then a '\t'. Make it iterate 50 times, and post the source if it does not do what you want it to.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    15
    if you want random values in a range try the formula

    Code:
    a + rand() % (b-a+1)
    where a = lower range
    b = upper range

    In your case a= 140, b = 160

  9. #9
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Just use the random func from the FAQ.

    Code:
    int random(int low, int high) { // my random function
    srand(time(NULL));
    return low + rand()%(high-low+1);
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21