### Random function

For my final project I am supposed to design a game of War. My main question is, after reading a file (Deck of 52) and initializing an array to have all the number cards. My question is, how do you split that array into two random hands?

Here's a sample of my code in case it is needed, but I just need to know how the random function works. And if there's any suggestions.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````/* This program will make a game of war between two players */ #include #include int main(void) { int i,j; //iterators int Deck[13][4];//array for the Deck of 52 int P1[13][2];//Player 1 array int P2[13][2];//Player 2 array FILE* inCards;//file to read inCards=fopen("cards.txt", "r"); if(inCards==NULL) { printf("Could not open file.");//either we typed it wrong or the file is fucked } else { for(i=0;i<13;i++)//Intialize the array { for(j=0;j<4;j++) {//Initialize the array and make sure it stops at the end of the file if (fscanf(inCards,"%d",&Deck[i][j])==EOF)//If the file reaches the end, it'll mark an error. { printf("error"); } } } } //FIND WAY TO RANDOMIZE THE DECK INTO TWO PLAYER HANDS fclose(inCards); 　 　 　 printf("******\n* *\n* %d *\n* *\n******");//Wartime Display return(0); }``````

Search for a 'rand' function on this site
Or...try a Fisher-Yates shuffle:
http://en.wikipedia.org/wiki/Fisher-Yates_shuffle

Then take the first half and make that the first player's hand and make the second half the second player's hand.
Last edited on
Topic archived. No new replies allowed.