-2

I am trying to get 2 different types of numbers/colors when I am calling my function twice, but it does not work. I've seeded the numbers/function but its still not working.

Here's how my code looks:

    #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define DIAMONDS 0
#define CLUBS 1
#define HEARTS 2
#define SPADES 3
#define JACK 11
#define QUEEN 12
#define KING 13
#define ACE 1
#define COLOR_SIZE 13
#define NR_OF_SUITS 4
#define DECK_SIZE 52


struct Card
{
    int suit;
    int value;
};

void printCards(struct Card *cardDeck);
void swapCards(struct Card *cardA, struct Card *cardB);
void shuffleCards(struct Card *cardDeck);
void printOneCards(struct Card *cardDeck);

int main()
{
    //struct Card deck[DECK_SIZE];      //Statiskt allokerad array
    struct Card * deck; //Dynamiskt allokerad array
    int index;
    int suit_index;

    deck = (struct Card *)malloc(sizeof(struct Card) * DECK_SIZE);
    for (suit_index = 0; suit_index < NR_OF_SUITS; suit_index++)    /* Initiera kortleken */
        for (index = 0; index < COLOR_SIZE; index++)
        {
            deck[suit_index*COLOR_SIZE + index].suit = suit_index;
            deck[suit_index*COLOR_SIZE + index].value = index;
        }
    printCards(deck);
    shuffleCards(deck);
    printCards(deck);
    printf("\n");
    printOneCards(deck);
    printf("Dealers cards\n");
    srand(time(NULL));
    printOneCards(deck);

    system("pause");
    return 0;
}

void printCards(struct Card *cardDeck)
{
    for (int i = 0; i < DECK_SIZE; i++)
    {
        switch (cardDeck[i].value + 1)
        {
        case ACE: printf("Ace ");
            break;
        case JACK: printf("Jack ");
            break;
        case QUEEN: printf("Queen");
            break;
        case KING: printf("King ");
            break;
        default: printf("%d ", cardDeck[i].value + 1);
            break;
        }
        printf("of ");
        switch (cardDeck[i].suit)
        {
        case DIAMONDS: printf("Diamonds ");
            break;
        case HEARTS: printf("Hearts ");
            break;
        case CLUBS: printf("Clubs ");
            break;
        case SPADES: printf("Spades ");
            break;
        default: printf("Something went wrong!! ");
            break;
        }
        printf("\n");
    }
}

void swapCards(struct Card * cardA, struct Card *cardB)
{
    struct Card temp;
    temp = *cardA;
    *cardA = *cardB;
    *cardB = temp;
}

void shuffleCards(struct Card *cardDeck)
{
    srand(time(NULL));

    for (int i = 0; i < DECK_SIZE; i++)
        swapCards(&cardDeck[i], &cardDeck[rand() % 52]);
}
void printOneCards(struct Card *cardDeck)
{
    srand(time(NULL));
    for (int i = 0; i < 2; i++)
    {
        switch (cardDeck[i].value + 1)
        {
        case ACE: printf("Ace ");
            break;
        case JACK: printf("Jack ");
            break;
        case QUEEN: printf("Queen");
            break;
        case KING: printf("King ");
            break;
        default: printf("%d ", cardDeck[i].value + 1);
            break;
        }
        printf("of ");
        switch (cardDeck[i].suit)
        {
        case DIAMONDS: printf("Diamonds ");
            break;
        case HEARTS: printf("Hearts ");
            break;
        case CLUBS: printf("Clubs ");
            break;
        case SPADES: printf("Spades ");
            break;
        default: printf("Something went wrong!! ");
            break;
        }
        printf("\n");
    }
}

So when I am calling the function printonecards(), it prints out 2 randomly cards and colors the first time, but the second time i call it, it does not. How do I fix this problem and how do I make it properly work?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Philly
  • 13
  • 1
  • 4

3 Answers3

2

You call srand a couple of times at the start of multiple functions. srand seeds the random number generator and should only be called when you wish to reset the random number generator. When you call srand using time(NULL) as the seed multiple times in the same second (which is quite possible considering current hardware), you will use the same seed with a clean random number generator. This will generate the same number multiple times.

The way to solve this is moving your call to srand to the start of main, then remove it in all other functions. This way your random number generator will be set exactly one, and won't be reset, producing more random numbers.

EDIT:

Another problem I didn't notice the first time, is that you pass the same argument to the function printOneCards, namely the argument deck. This argument doesn't differ between the cards of the player and the cards of the dealer, meaning the function will print the same deck twice.

Since a player always seems to have 2 cards, you can use pointer arithmetic to change this. Your function calls will then become:

printOneCards(deck); //Print the first and second card
printf("Dealers cards\n");
printOneCards(deck+2); //Print the third and fourth card

This way you will get different results for both the player and the dealer, as you intended.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Shadowwolf
  • 973
  • 5
  • 13
  • Ive tried as you've written in your answer. but it still doesnt work. I have removed srand from all other functions except main, but it still gives me the same sequence of numbers. – Philly Dec 27 '15 at 13:18
  • Did you move srand to the top of main? Try writing it as the first action you take in main. – Shadowwolf Dec 27 '15 at 13:53
  • Yep, i've done that. It doesnt matter though, if i still call the function twice it gives me nothing but the same two numbers printed. – Philly Dec 27 '15 at 14:04
  • Edited so you won't have that problem anymore. – Shadowwolf Dec 27 '15 at 14:20
0

TL;DR You need to seed the PRNG (Pseudo Random Number Generator) only once at the start of your program. Remove srand(time(NULL)); from the functions and put it into your main().

To elaborate, time(NULL) has a time granularity of 1 second. which means, if called multiple times within 1 second, it will return the same value. This way, you'll end up seeding the PRNG with same value again and again and thus, the pseudo random numbers generated by rand() will be same.

That's why, you need to remove the seeding part from your called function to avoid re-seeding the PNRG.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

You can use srand() all time you need to seed a new random number. If you put the srand(time(NULL)) on for loop or while loop, maybe you get what you want.

OppaiVolk
  • 1
  • 1