Jump to content
  • Advertisement
Sign in to follow this  
daviangel

mod operator vs integer division in C/C++

This topic is 3785 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering what the practical differences are between using the mod vs integer division truncation as in this example. card = rand0toN(52); // get random from 0 to 51 r = card % 13; // random from 0 to 12 s = card / 13; // random from 0 to 3 ?
// Jaime Moreno 2/6/08

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rand0toN(int n);
void draw_a_card();

// use 2 character arrays to hold cards
char* suits[4] = {"hearts", "diamonds", "spades", "clubs"};
char* ranks[13] = {"ace", "two", "three", "four", "five",
                   "six", "seven", "eight", "nine",
                   "ten", "jack", "queen", "king"};
int main()
{
  int n; // number of cards to draw

  srand(time(0)); // setup random number generator

  while(1)
  {
    cout << "Enter no. of cards to draw (0 to exit):";
    cin >> n;
    
    if(n == 0)
      break; // exit
    for(int i = 1; i <= n; i++) // loop as many times as user picked
      draw_a_card();
  }

  return 0;
}

// random 0ton function
int rand0toN(int n)
{
  return rand() % n;
}

// Draw a card function
/* Performs one card-draw by getting a random 0-3 and
   a random 0-12. These are then used for the index 
   for the string arrays, ranks and suits.
*/
void draw_a_card()
{
  int r;  // random index(0-12) into ranks array
  int s;  // random index(0-3) into suits array
  int card;

  card = rand0toN(52); // get random from 0 to 51
  r = card % 13; // random from 0 to 12
  s = card / 13; // random from 0 to 3 ?
  cout << ranks[r] << " of " << suits[s] << endl;
}



The reason I ask is that I would've just used s = card % 4 myself. This is from "C++ without fear" book which I'm going over right now to brush up on my C++ and I have no clue why the author would switch from using the mod operator to integer division here?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
card = rand0toN(52); // get random from 0 to 51
r = card % 13; // random from 0 to 12
s = card / 13; // random from 0 to 3 ?

r and s aren't random numbers; they're a function of card.

If you were to count through each of the 52 possible values of card, r increments once per card up to 12 before wrapping back to 0, and s increments once per every 12 cards.
In other words, the 52 cards are sorted first by suit, and within each suit they are sorted by rank.

In this case it's the same as writing:

card = rand0toN(52); // get random from 0 to 51
if( card < 13 ) { //it's a heart
r = card
s = 0;
}
else if( card < 26 ) {//it's a diamond
r = card-13
s = 1;
}
else if( card < 39 ) {//it's a spade
r = card-26
s = 2;
}
else {//it's a club
r = card-39
s = 3;
}

Quote:
Original post by daviangel
The reason I ask is that I would've just used s = card % 4 myself.

that would give different (incorrect) results...
e.g.
for card 3 their way gives : r=3, s=0 (the four of hearts)
for card 3 your way gives : r=3, s=3 (the four of clubs!)

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Quote:
card = rand0toN(52); // get random from 0 to 51
r = card % 13; // random from 0 to 12
s = card / 13; // random from 0 to 3 ?

r and s aren't random numbers; they're a function of card.

If you were to count through each of the 52 possible values of card, r increments once per card up to 12 before wrapping back to 0, and s increments once per every 12 cards.
In other words, the 52 cards are sorted first by suit, and within each suit they are sorted by rank.

In this case it's the same as writing:*** Source Snippet Removed ***
Quote:
Original post by daviangel
The reason I ask is that I would've just used s = card % 4 myself.

that would give different (incorrect) results...
e.g.
for card 3 their way gives : r=3, s=0 (the four of hearts)
for card 3 your way gives : r=3, s=3 (the four of clubs!)

Thanks for the helpful example but I guess I gotta finish reading this chapter to figure out what the author is up to.
I know r and s aren't random in my example but it looks like I forgot to change my comments because I copied most of the code from my first dealer.cpp file in which r ans s were determined by a call to rand0toN().
First version of draw_a_card didn't use a card variable at all and used this instead:
r = rand0toN(13);
s = rand0toN(4);
That's why I'm confused because the new draw_a_card() still is still going to return an index of 0-12 for my rank array and 0-3 for my suits char array. So to me not much has changed except that I'm only making one call to rand0toN now but I'm guessing the next version is going to make use of an 52 element array in which case it would make alot more sense.
Okay well I just saw why my way won't work without making another call to rand0toN which is what we are trying to avoid since:
s = card % 4; is not the same as s = rand0toN(4); which is what I was thinking or is it?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!