help

This is a discussion on help within the C++ Programming forums, part of the General Programming Boards category; ok i am having a little trouble generating random numbers betwen a range. i want the number to be between ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    26

    help

    ok i am having a little trouble generating random numbers betwen a range. i want the number to be between 40 and SCREEN_X-40 and also be a factor of 20. I had it working, but i somehow screwed it up and saved over it and cant get it working.

    this is what i have, but it is not doing #'s in the rt range

    x = 20*((rand() % ((SCREEN_W-40)/20) +2 ));

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    40 <= x <= SCREEN_X - 40
    0 <= x <= SCREEN_X - 80
    therefore: (rand() % (SCREEN_X - 80)) + 40

    i think

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    26
    that creates numbers in the rt range, but they are not factores of 20. also why is it SCREEN_X-80.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    26
    ok i figured it out
    x=20*((rand() % (SCREEN_X - 80)/20) + 2);

    thanks for your help

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