Thread: Discrepancy using rand() and array.

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Discrepancy using rand() and array.

    I've made the following function.
    Code:
    void select(int deck[52])
    {
    	srand(time(NULL)); //Seeds the randomization
    	int random = rand() % 52; //Puts a random value in the variable random.
    	int selected;
    	printf("\n%d\n", random);
    	selected = deck[random];
    	printf("\n%d\n", selected);
    	
    }
    The problem is that e.g. it printed it had used 50 as the random value. The corresponding deck-card would be: 12. It gives me 13, however.

    I thought that the rand() function would give me values from 1 and up (correct?)?

    I've thought about this, but I can't figure it out. What could be wrong?

    Thank You.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Nope, it starts at 0:
    Quote Originally Posted by MSDN
    The rand function returns a pseudorandom integer in the range 0 to RAND_MAX (32767).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The range of rand() is [0, RAND_MAX].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In other words, both 0 and RAND_MAX might be returned by rand().

    Note that srand() should only be called once during the execution of a program, so you should probably put it in an initialization function of some sort.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Even if rand() gave you values of one or greater, when you take the modulus of that number, zero will still be one of the outcomes. E.g. 52 % 52 equals zero.

    Also, you need to make sure that you only call srand once in your entire program, or rand() will not work correctly! Best to put it at the start of main rather than in a function that can be called more than once.
    Please read the FAQ about this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by iMalc View Post
    Even if rand() gave you values of one or greater, when you take the modulus of that number, zero will still be one of the outcomes. E.g. 52 % 52 equals zero.
    Yes . . . rand() % N will give you N possible numbers, from 0 to N-1 inclusive. In other words, rand() % 4 might give you 0, 1, 2, or 3. One way of looking at it is the number you use for the modulus is just like the number you use to declare an array.
    Code:
    int array[5] = {1, 2, 3, 4, 5};
    
    printf("%i\n", array[rand() % 5]);
    Also, you need to make sure that you only call srand once in your entire program, or rand() will not work correctly! Best to put it at the start of main rather than in a function that can be called more than once.
    Please read the FAQ about this.
    I did just mention that, but I guess it's important enough to be repeated.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yeah I'm terrible when it comes to cross-posts. I often open up many windows of things that seem interesting, and then answer them one at a time. I can easily be 20 minutes before I get around to posting after initially opening the window.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed