Srand () w/ range

This is a discussion on Srand () w/ range within the C Programming forums, part of the General Programming Boards category; Code: srand(time(NULL)); range = (15-9) + 1; printf(" %d", rand() % range + 9); That code was for a range ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    Post Srand () w/ range

    Code:
         srand(time(NULL));
         range = (15-9) + 1;
         printf(" %d",   rand() % range + 9);
    That code was for a range between 9 to 15, but I'm having problems with getting it into the negative range. The range I'm trying to get is -5 to 14.

    Any help is appreciated, thank you in advance.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The range I'm trying to get is -5 to 14.
    Code:
         srand(time(NULL));
         range = (14 - -5) + 1;
         printf(" %d",   rand() % range - 5);

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Do it after, since rand()'s return value is 0 to RAND_MAX.

    [edit]nevermind...[/edit]

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    awesome, thanks swoopy. I wasn't sure if 14 - - 5 would work, because i would assume it be 20. but then writing out the number % 20 - 5 would work as well. Thanks!

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I guess to generalize you could write:
    Code:
    num = (rand() % (upper - lower + 1)) + lower

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    I want to be able to expand what we're doing. how would I go about only choosing intervals randomly from the range?
    So between -5 to 14 but intervals of 3. Selecting only the possibilities of :
    -5, -2, 1, 4, 7, 10, 13

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well you've got 7 different numbers so:
    Code:
    num = 3 * (rand() % 7) + lower;
    lower would be -5 in this case.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Starting from 0,
    Code:
    /* interval of 3 from 0 */
    if(num % 3 == 0)
    Now adjust for starting from -5

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    awesome, thanks guy ;D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 09:46 AM
  3. Please help to check.
    By nicoleha in forum C Programming
    Replies: 16
    Last Post: 12-07-2005, 02:29 PM
  4. random number in range
    By lgg in forum Linux Programming
    Replies: 3
    Last Post: 08-14-2005, 05:15 AM
  5. Random Numbers within a range OTHER than 1-X
    By Kaelin in forum C++ Programming
    Replies: 11
    Last Post: 02-16-2005, 10:57 AM

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