简体   繁体   中英

Rand is still not working when im seeding the number

Note, the code below is not completely written out, I've already made a deck and shuffled the cards

I don't understand why i don't get two random numbers, I've tried to seed the numbers but it doesn't seem to work properly. What i would like is for everytime i print out face/suit it should be two different numbers/colors. Where are my mistake?

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

struct card{
    const int *face; 
    const char *suit; 
};
typedef struct card Card;

void dealing(const Card * const wDeck);
void shuffle(Card * const wDeck);

int main(){
    srand(time(NULL));
    shuffle(deck);
    dealing(deck);
    return(0);
}

void dealing(Card * const wDeck)
{
    int j;
    j = rand() % 52;
    srand(time(NULL));

    printf("%d of %s\n", wDeck[j].face, wDeck[j].suit);
    printf("%d of %s\n", wDeck[j].face, wDeck[j].suit);
} 
void shuffle(Card * const wDeck)
{
    int i;     
    int j;    
    Card temp; 

    for (i = 0; i <= 51; i++) {
        j = rand() % 52;
        temp = wDeck[i];
        wDeck[i] = wDeck[j];
        wDeck[j] = temp;
    } 
} 

Remove the call to srand() from within dealing() . Calling srand() once per program invocation (in main() ) is enough.

    void dealing(Card * const wDeck)
    {
        int j;
        j = rand() % 52;
        // srand(time(NULL)); /* no, no, no.
                              /* The PRNG has already been
                              /* seeded inside main() */

        printf("%d of %s\n", wDeck[j].face, wDeck[j].suit);
        printf("%d of %s\n", wDeck[j].face, wDeck[j].suit);
    } 
void dealing(Card * const wDeck)
{
    int j;
    j = rand() % 52;
    srand(time(NULL));

    printf("%d of %s\n", wDeck[j].face, wDeck[j].suit);
    //                        ^^^            ^^^
    printf("%d of %s\n", wDeck[j].face, wDeck[j].suit);
    //                        ^^^            ^^^
}

You're printing values of the j th element twice.

dealing is problematic in several ways.

Firstly, you are calling srand() every time you want a random number. The way rand works is that it starts off with a seed number and performs a complicated piece of arithmetic on it each time it has to produce a new "random" number. For this reason, the sequence isn't really random (that's why they are called pseudo random number generators (PRNGs).

You are reseeding the sequence with the current time in seconds every time you ask for a random number. Now assuming your function deals the cards in a tight function with no artificial delays, is it not likely that you will always be reseeding the PRNG with the same time ? As pmg says, call srand() only once per run of your program.

Secondly, and I am assuming this is a typo, you forgot to generate a new random number before selecting the second card. (This is pmg's other answer.) Actually, as you have already shuffled the pack, why not just select the first two cards from the deck?

You don't say what platform you are using, but on some (like OS X), srand/rand is a very poor random number generator. You should use srandom/random if you have got it. Even better, you should be using arc4random_uniform() if your platform has got it. This is because rand() % 52 introduces a bias because 52 is not a power of 2. Some numbers are more likely to come up than others.

Finally, your shuffle looks fine to me, but it might be biased. I would use the Fisher Yates shuffle which is known to be unbiased.

void fisherYatesShuffle(Card* const deck, size_t cardCount)
{
    for (size_t i = 0 ; i < cardCount ; ++i)
    {
        size_t upperCardIndex = cardCount - i - 1;
        size_t pickIndex = arc4random_uniform(upperCardIndex + 1);
        Card tmp = deck[upperCardIndex];
        deck[upperCardIndex] = deck[pickIndex];
        deck[pickIndex] = tmp;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM