I am trying to write a code which randomizes numbers between 1 and 14 (symbolizing one suit of a deck of cards). The code should store the values in an array with a limit of 52. Each number can only be stored 4 times (as there are 4 suits in a deck of cards). So, in the end, I should be displaying two randomized decks for person_a and person_b.
My problem is that the randomized decks for person_a and person_b are the same. I have no idea why. I tried seeding using srand(), and using rand() for the random number. Can someone help?
Also, I know my code is really messy and terrible. I'm sorry - this is my first time taking a C course. Below is the code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define MAX_DECK 52
#define REPETITIONS 4
#define CARDS_HIGH 14
#define CARDS_LOW 1
int
randomize_check(int value_check, int limit, int cards[])
{
int count = 0;
int i = 0;
for(i=0; i<limit; i++)
{
if(cards[i]==value_check)
{
count++;
}
}
if(count>REPETITIONS)
{
return -1;
}
else if (count<=REPETITIONS)
{
return 1;
}
}
int
get_random(void)
{
int random_number = 0;
random_number = (rand()%(CARDS_HIGH-CARDS_LOW))+CARDS_LOW;
return(random_number);
}
int * randomize_deck(void)
{
static int cards[MAX_DECK];
int i = 0;
int randomize = 0;
int check = 0;
for (i=0; i<MAX_DECK; i++)
{
randomize = get_random();
cards[i] = randomize;
check = randomize_check(cards[i], MAX_DECK, cards);
while((check) == -1)
{
randomize = get_random();
cards[i] = randomize;
check = randomize_check(cards[i], MAX_DECK, cards);
}
}
return(cards);
}
int
main(void)
{
srand ( time(NULL) );
int i = 0, j = 0;
int *person_a = randomize_deck();
int *person_b = randomize_deck();
for (i = 0; i < MAX_DECK; i++) //print_a
{
printf( "Cards[a%d]: %d\n", i, *(person_a + i));
}
printf("\n");
for (j = 0; j < MAX_DECK; j++) //print_b
{
printf( "Cards[b%d]: %d\n", j, *(person_b + j));
}
return(0);
}