Question about getting random number in range

This is a discussion on Question about getting random number in range within the C Programming forums, part of the General Programming Boards category; Hi, I got the following line from the FAQ: rc = (rand() % (max - min + 1) + min); ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    31

    Question about getting random number in range

    Hi, I got the following line from the FAQ:

    rc = (rand() % (max - min + 1) + min);

    I know this does work but I'd rather know why my code is working. I'm not very mathmatically minded so if anyone could give me a bit of a walkthrough that'd be great.

    Thanks

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Try working it thru for some dice rolls. Good ol D&D nomenclature....

    d6.....

    rand() % ( 6-1+1) will give a number in range 0 to 5 adding 1 raises range to 1 to 6 i.e. d6

    3d6

    rand() % (18-3+1) will give you a number in range 0 to 15. adding 3 raises range to 3 to 18 or 3d6

    starting to see how this works. When you understand the modulo operator everything else will fall into place.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Need to watch your () there Stoned_Coder

    rand() % (6-1+1) would be the same as rand() % 6
    rand() % (6-1) + 1 will give you the right results

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    err no thantos try again
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    31
    Thanks it's clearer now

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    err yes Stoned_Coder

    (6-1+1) is 6

    (18-3+1) is 16

    () are done before % which is done before +

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by Stoned_Coder
    err no thantos try again
    i think you missed that he was making fun of you
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    srand(time());
    number=rand()%<enter the number u want it to go up to here>;

    make sure to #include<time.h>

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    err yes Stoned_Coder

    (6-1+1) is 6

    (18-3+1) is 16

    () are done before % which is done before +
    you were misusing the modulus operator.
    Remember the first example I gave was for 1d6. My figures were correct. If you had thought about it you would have seen it.

    6-1+1 is indeed 6. so rand()% 6 is what we are saying. This gives a random number in range 0 to 5 then we add min which in this case is 1 so range become 1 to 6.

    You suggested for 1d6 that i want ....

    rand() % (6-1) + 1

    Lets look at this....

    rand() % 5 gives us 0 to 4 then add 1 so 1 to 5. is that correct figures for 1d6 ?

    so thantos looks like you were brainfarting after all
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok so mine would have given 1 - 5, yours would have given 0 to 5. Both examples would have been wrong unless you purposly left off showing the + 1. Which is very easy for me to not see at 5:30 in the morning

    But it really doesn't matter as using % for rand() is the lazy way that results in poor numbers.

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    quite right i was only showing this part of the eq....

    rand() % (max-min+1)

    Then i went on to describe adding min, didnt mathematically show it but even so you should have read my post properly and it is all explained.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Read? You mean I can't just look at the pretty dancing numbers?

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Ok. here ya go, just for you....
    Attached Images Attached Images  
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number array
    By matt_570 in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2008, 04:44 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  3. Question for Random class
    By joenching in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2005, 11:22 PM
  4. random number checking...
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2005, 12:51 PM
  5. random number
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-16-2003, 08:04 PM

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