1. ## playing ... cards??

hey People...
I was trying to make a program that would play cards, BUT (there is always a butt - the random function (to shuffle the cards) could work forever if it wont pick all the cards...
you see I used random(52); and then switch case....
now if random doesn't pick them all the loop continues...

Well I know it sounds stupid.. but thats me, and I'm looking for a code that picks cards better then random...

2. i know this would be maybe too much work, but here's an idea:
make a list of all numbers, 1-52 in order
randomly pick a number(really random)
cut the number from the list, choose again from the remaining 51 numbers in the list

with an array you could make a function which would cut a number and move every number after that down to fill the gap. or, with a linked list, you could just attach the previous pNext pointer to one further down the list. but a linked list needs structs, and that seems like too much for a simple card choosing program.

3. ## try this...

Thanks BUT.. I've done a list and just scrambeld it a little using random... can you tell me if that's ok???

r=random(20);
for(i=0;i<52;i++)
{
while((i+r)>52) r=random(20); // makes sure the random
temp=cards[i]; // has limits!!!
cards[i]=cards[i+r];
cards[i+r]=temp; // Shuffels the cards...
}

4. from what i see it looks ok. here's what i think's happening(tell me if i'm wrong):
Code:
```r=random(20);
for(i=0;i<52;i++)
{
while((i+r)>52) r=random(20); // makes sure the random
temp=cards[i]; // has limits!!!
cards[i]=cards[i+r];
cards[i+r]=temp; // Shuffels the cards...
}```
each card (i) is swapped with (i+r), which is basically another card sometime after (i). it's not really random that way, but i'm assuming you're looking for an imperfect way to do this. i'll compile this (with declarations, etc...) and try it out.

5. i've compiled this program(basically your code sample with extras to make it compile):
Code:
```#include <stdlib.h>
#include <time.h>
#define random(x) (rand()%x)
int main()
{

int i=0;
int r=0;
int temp=0;
int cards[52];
srand(time(NULL));
for (i=0;i<53;i++) { cards[i]=i; }

r=random(20);
for(i=0;i<52;i++)
{
while((i+r)>52) r=random(20); // makes sure the random
temp=cards[i]; // has limits!!!
cards[i]=cards[i+r];
cards[i+r]=temp; // Shuffels the cards...
}
for (i=0;i<52;i++) {
printf("%i      %i\n",cards[i],i); }

return 0;
}```
the results are very sequential and not random. i think you need a better algorithm. and it looks like you may be swapping an extra card(cards 0-52 instead of 0-51). try making the while((i+r)>52 statement into while((i+r)>=52.

6. ## shuffle cards

This sample seems to get the job done... Deitel & Deitel series

Code:
```for( int i = 0; i < MAX_CARDS; i++)
{
randIndex = rand() % MAX_CARDS;

temp                        = cards[ i ];
cards[ i ]                  = cards[ randIndex ];
cards[ randIndex ]   = temp;
}```

7. correction....

for( i = 0; i < MAX_CARDS; i++)
{
randIndex = rand() % MAX_CARDS;

temp = cards[ i ];
cards[ i ] = cards[ randIndex ];
cards[ randIndex ] = temp;
}

8. Code:
```#include <stdio.h>
#include <time.h>

int getCurrent( int c[], int num )
{
int x,y,z;
for( y = -1, x = 0; x < 52; x++ )
{
if( c[x] > -1 ) y++;
if( y == num )
{
y = c[x];
c[x] = -1;
return y;
}
}
return -1;
}

int main( void )
{
int cards[52], deck[52], count, current;
srand( time(0) );

for( count = 0; count < 52; count++ )
{
cards[count] = count;
}

for( count = 0, current=52; count++ )
{
deck[count] = getCard( card, rand() % current-- );
if( deck[count] == -1 )
{
error...exit or do whatever, shouldn't ever happen
}
}
}```
I'm sure this is your homework assignment, but I'm tired, and don't care. You luck out today. Anyway, this should work. I haven't tried it, and keep in mind I've only had 2 hours sleep in the past day, so use at your own risk.

Quzah.

9. Watch the array going OOB as declared as 52 ie 0-51 but array loops, random numbers generated to 52 (should be 51).

10. > Watch the array going OOB as declared as 52 ie 0-51 but array
> loops, random numbers generated to 52 (should be 51).

Oh really? How do you figure:

> for( count = 0, current=52; count++ )
> {
> deck[count] = getCard( card, rand() % current-- );

rand % 52 is zero to 51. You are incorrect.

Quzah.

11. > for( count = 0, current=52; count++ )

I wonder what this does?

12. > for( count = 0, current=52; count++ )

Ah, well, there you have it. You're an idiot. You obviously cannot tell what a typo is. If so, you're realize that the above line was supposed to be (OBVIOUSLY):

for( count = 0, current=52; count < 52; count++ )

Hell, that (the first) won't even compile.

You obviously missed the part where I said:
Anyway, this should work. I haven't tried it, and keep in mind I've only had 2 hours sleep in the past day, so use at your own risk.
It's not my fault you can't read, or use common sense.

In any case, they're STILL wrong, because they stated that the random number goes to 52. No, it doesn't. It starts at MOD 52 and goes downward. You still aren't going over 52. If the looping is off, the most likely thing to happen is a divide by zero error. In which case, they're still wrong, because the random number does NOT go above 51.

Quzah.

13. Sorry.

How dare I correct your mistake and try to help the person who came here to ask a question.

Will know better in the future.

14. > Sorry.
>
> How dare I correct your mistake and try to help the person who
> came here to ask a question.

The problem isn't that you tried to point out a mistake. The problem is that you acted like you knew what you were talking about, when clearly you didn't.

You didn't "point out" anything. You didn't quote code. You didn't show a fix. You didn't even have the right "solution". Simply put: You said the problem was with the random number. This is wrong. There was nothing at all wrong with the random number aspect of the program.

If you're going to try and point out errors, then by all means, POINT THEM OUT. Even better, make sure you're right when you're doing it. If you say something is wrong, point it out (quoting usually is a GoodThing(TM)).

The fact remains that you were wrong. Nothing was wrong with the "random" part of the program, which is what you said was wrong.

Quzah.

15. Originally posted by quzah

Ah, well, there you have it. You're an idiot. You obviously cannot tell what a typo is. If so, you're realize that the above line was supposed to be (OBVIOUSLY):
You're scaring me again.