Click to See Complete Forum and Search --> : Random Cards


prince_kenshi
03-23-2001, 02:09 PM
I'm trying to make a card program and I want it to draw 20 random cards at once. Basically it gets a random number between 1 and 13 and will be changed to a card later. The random seed seems to only change each second but I want all the cards at once. Since just getting a random number with rand() got the same number for each card, I made a more complex algorithm, but it's still the same card each time. All I want is something complex enough that the player can't figure out what the next card will be, even if he knows the program's source. Can someone take a look at this segment of it and tell me why it keeps repeating cards and how to fix it? (It draws different cards each time I execute it, just not throughout the same execution.)



short int my_hand[20,2];
short int comp_hand[20,2];
char cards[2];
int seed2;
srand((unsigned)time(NULL));
seed2=rand()%100+50;
for(cards[0]=0;cards[0]<20;cards[0]++)
my_hand[cards[0],0]=(rand()+(cards[0]*seed2)^2)%13+1;
for(cards[0]=0;cards[0]<20;cards[0]++)
comp_hand[cards[0],0]=(rand()+(cards[0]*seed2)^2)%13+1;


[ 23 March 2001: Message edited by: prince_kenshi ]

Stuka
03-23-2001, 02:37 PM
Not exactly the same function, but similar, and maybe you can use it. Running this in Windows didn't give the same result every time, but that may be system dependent.
for (int card = 0; card < 5; ++card)
{
row = rand() % 4;
column = rand() % 13;

if (wDeck[row][column] != 1)
{
cout << setw(5) << setiosflags(ios::right)
<< wFace[column] << " of "
<< setw(8) << setiosflags(ios::left)
<< wSuit[row]
<< (card % 2 == 0 ? '\t' : '\n');
wDeck[row][column] = 1;
continue;
}
--card;
}
cout << "\n\n";

prince_kenshi
03-23-2001, 05:26 PM
I'm not using C++ though, only C. I tried assigning random numbers for each card with only the rand() function, but then I have all the same card and the computer has all the same card. So I tried this more advanced formula and they're still all the same. Thanks.