rand

This is a discussion on rand within the C Programming forums, part of the General Programming Boards category; will this code: int randInt = rand() % 52; ever hit the number 52??...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    46

    rand

    will this code:

    int randInt = rand() % 52;

    ever hit the number 52??

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    No.

    If you divide anything by 52, will the remainder ever be 52?

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    He could have. That's just a code snippet.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by pelicanpie View Post
    it won't because you forgot to seed the generator
    Wouldn't matter. The only thing the seed does is give you a "random" starting point. There's a 1/52 chance that the default seed gives you any particular number. So, assuming there really wasn't an issue with % 52 being impossible to generate 52, it would have been possible. (IE: If he had said, is there ever a chance it's 5, then yes, there is a 1/52 chance that the default seed spits out 5.)


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    ok, so then my next question is...
    How can I have it spit out a random number from 1 to 52? both included...

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    What's the range you get from % 52?

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    0-51....the modulus operator is essentially saying "Whatever divided by 52 and the remainder is..." so 0-51...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Yes, we know . I am just trying to get the OP to answer his own question step by step.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    While that gives you slightly better distribution, (since RAND_MAX may not be divisible by 52), it will be VERY slow.

    A far faster way would be
    int r = rand() % 52 + 1;

    If you don't need perfect uniformity (some values will occur very very slightly more frequently than others, most certainly undetectable for general use).

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    That's not any faster if you include the time used to pregenerate the random numbers .

    Have you actually tried running your code? It can take a few hours to return one value on implementations that have large RAND_MAX. Even with a minimum of 2^15 allowed by the standard, it would require ~500 calls to rand() on average to get a number in range.

    rand() is a very slow function already, in most implementations.

    And also, seeding the generator with current time on every call is a BAD idea. What if you make 2 calls within 1 second?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 06:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 02:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand() technical question
    By freakme in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 10:22 PM

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