1.What's the difference between this while(1) and this while(true)?

2.r = card % 13; // r = random 0 to 12

s = card / 13; // s = random 0 to 3 how's that card / 13 is random 0 to 3

someone please answer.

2.r = card % 13; // r = random 0 to 12

s = card / 13; // s = random 0 to 3 how's that card / 13 is random 0 to 3

someone please answer.

1) They are exactly the same.

2) It depends what card is, and I don't see how dividing by 13 gives you a random # from 0 to 3 either.

2) It depends what card is, and I don't see how dividing by 13 gives you a random # from 0 to 3 either.

`true`

is defined as "not-zero". Therefore 1 is `true`

. Likewise 2 is `true`

and 3 is `true`

, etc. Only 0 is

`false`

.Why would you use

`while`(1)

when you can use `while`(`true`)

? It doesn't make a difference, but in C there is no true or false so you'd have to do `while`(1)

if you are writing C code. There may be a few of these left over if your code was ported from C or was written by someone who is more used to C.
@firedraco It depends what card is, and I don't see how dividing by 13 gives you a random # from 0 to 3 either.

icoR

Well here's the code.I also don't see how dividing by 13 gives a random # from 0 to 3 either.

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <cmath>

using namespace std;

int rand_0toN1(int n);

void draw_a_card();

int select_next_available(int n);

bool card_drawn[52];

int cards_remaining = 52;

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, i;

srand(time(NULL)); // Set seed for randomizing.

while (1) {

cout << "Enter no. of cards to draw ";

cout << "(0 to exit): ";

cin >> n;

if (n == 0)

break;

for (i = 1; i <= n; i++)

draw_a_card();

}

return 0;

}

// Draw-a-card function

// Perform a card draw by getting a random 0-4 and a

// random 0-12. Use these to index the strings

// arrays, ranks and suits.

//

void draw_a_card() {

int r; // Random index (0 thru 12) into

// ranks array

int s; // Random index (0 thru 3) into

// suits array

int n, card;

n = rand_0toN1(cards_remaining--);

card = select_next_available(n);

r = card % 13; // r = random 0 to 12

s = card / 13; // s = random 0 to 3

cout << ranks[r] << " of " << suits[s] << endl;

}

// Select-next-available-card function.

// Find the Nth element of card_drawn, skipping over

// those elements already set to true.

//

int select_next_available(int n) {

int i = 0;

// At beginning of deck, skip cards already drawn.

while (card_drawn[i])

i++;

while (n-- > 0) { // Do the following n times:

i++; // Advance to next card

while (card_drawn[i]) // Skip past cards

i++; // already drawn.

}

card_drawn[i] = true; // Note card to be drawn

return i; // Return this number.

}

// Random 0-to-N1 Function.

// Generate a random integer from 0 to N–1.

//

int rand_0toN1(int n) {

return rand() % n;

}

icoR

Well here's the code.I also don't see how dividing by 13 gives a random # from 0 to 3 either.

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <cmath>

using namespace std;

int rand_0toN1(int n);

void draw_a_card();

int select_next_available(int n);

bool card_drawn[52];

int cards_remaining = 52;

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, i;

srand(time(NULL)); // Set seed for randomizing.

while (1) {

cout << "Enter no. of cards to draw ";

cout << "(0 to exit): ";

cin >> n;

if (n == 0)

break;

for (i = 1; i <= n; i++)

draw_a_card();

}

return 0;

}

// Draw-a-card function

// Perform a card draw by getting a random 0-4 and a

// random 0-12. Use these to index the strings

// arrays, ranks and suits.

//

void draw_a_card() {

int r; // Random index (0 thru 12) into

// ranks array

int s; // Random index (0 thru 3) into

// suits array

int n, card;

n = rand_0toN1(cards_remaining--);

card = select_next_available(n);

r = card % 13; // r = random 0 to 12

s = card / 13; // s = random 0 to 3

cout << ranks[r] << " of " << suits[s] << endl;

}

// Select-next-available-card function.

// Find the Nth element of card_drawn, skipping over

// those elements already set to true.

//

int select_next_available(int n) {

int i = 0;

// At beginning of deck, skip cards already drawn.

while (card_drawn[i])

i++;

while (n-- > 0) { // Do the following n times:

i++; // Advance to next card

while (card_drawn[i]) // Skip past cards

i++; // already drawn.

}

card_drawn[i] = true; // Note card to be drawn

return i; // Return this number.

}

// Random 0-to-N1 Function.

// Generate a random integer from 0 to N–1.

//

int rand_0toN1(int n) {

return rand() % n;

}

@Stewbond

I think in the book I read they said it's the same,but sometimes they use while(1) and sometimes while(true),

so I taught there must be some difference.

I think in the book I read they said it's the same,but sometimes they use while(1) and sometimes while(true),

so I taught there must be some difference.

There is a better way to handle cards I have learned over the years. Cards represented by a number between 0 and 51. (I made it 1 - 52 so 0 could represent a card back and 53 - 54 could represent the jokers. Now how do you get the card number? I will use the original 0 - 51 example.

unsigned int card;

int face = (card / 13) + 1;

int suit = card % 4;

Now you have a number in suit from 0 to 3 while in suit you have a number between 1 and 13. Just assign values for suit like 0 is clubs, 1 is hearts, 2 is diamonds and 3 is clubs.

This way you only have one random number to worry about instead of two.

unsigned int card;

int face = (card / 13) + 1;

int suit = card % 4;

Now you have a number in suit from 0 to 3 while in suit you have a number between 1 and 13. Just assign values for suit like 0 is clubs, 1 is hearts, 2 is diamonds and 3 is clubs.

This way you only have one random number to worry about instead of two.

Hi darkovasic,

In the future, please post using "code tags" (they're by*Format:* when you post a new thread or a reply.) It makes code easier to read.

The comment is misleading. "s" is not set to a random number from 0 to 3, it is set to either 0, 1, 2, or 3 depending on which__random card __is selected.

The program uses a boolean array (

The variable "card" is set equal to a number from 0 to 51, depending on what select_next_available returns (which is random, sorta).

So "card" is at least 0, and at most 51.

If we selected card 51, then 51 / 13 = 3.

If we selected card 30, then 30 / 13 = 2.

If we selected card 14, then 14 / 13 = 1.

Also, 0 / 13 = 0.

Since we set s = card / 13, s will be a number from 0 to 3. S isn't random, but "card" is, and the range of card gives a range of 0, 1, 2, and 3 for "s".

The logic for "r" is very similar.

51 % 13 = 12.

0 % 13 = 0

and we can have all the values in between. "r" isn't random, but "card" is.

In my opinion, that code isn't good...and the comments are outright misleading.

In the future, please post using "code tags" (they're by

The comment is misleading. "s" is not set to a random number from 0 to 3, it is set to either 0, 1, 2, or 3 depending on which

The program uses a boolean array (

`bool` card_drawn[52];

) to keep track of which cards have already been drawn. In a sense, that means the cards are numbered from 0 to 51 (slot 0, slot 1, slot 2...slot 51).The variable "card" is set equal to a number from 0 to 51, depending on what select_next_available returns (which is random, sorta).

So "card" is at least 0, and at most 51.

`s = card / 13;`

If we selected card 51, then 51 / 13 = 3.

If we selected card 30, then 30 / 13 = 2.

If we selected card 14, then 14 / 13 = 1.

Also, 0 / 13 = 0.

Since we set s = card / 13, s will be a number from 0 to 3. S isn't random, but "card" is, and the range of card gives a range of 0, 1, 2, and 3 for "s".

The logic for "r" is very similar.

`r = card % 13;`

51 % 13 = 12.

0 % 13 = 0

and we can have all the values in between. "r" isn't random, but "card" is.

In my opinion, that code isn't good...and the comments are outright misleading.

Last edited on

Topic archived. No new replies allowed.