简体   繁体   English

rand()在第一次之后返回相同的值

[英]rand() returns the same value after the first time

First, here's my code: 首先,这是我的代码:

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

#define NUM_SUITS 4
#define NUM_RANKS 13

bool in_hand[NUM_SUITS][NUM_RANKS] = {false};
bool newcard = {false};
int num_cards, rank, suit, totrank;
const char rank_code[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K',};
const char suit_code[] = {'C','D','H','S'};

int main_hand ()
{
    suit = rand() % NUM_SUITS;
    rank = rand() % NUM_RANKS;
    if (!in_hand[suit][rank]) {
        in_hand[suit][rank] = true;
        num_cards--;
        if (suit == 0){
            printf("%c of Clubs \n", rank_code[rank]);
        }
        else if (suit == 1){
            printf("%c of Diamonds \n", rank_code[rank]);
        }
        else if (suit == 2){
            printf("%c of Hearts \n", rank_code[rank]);
        }
        else if (suit == 3){
            printf("%c of Spades \n", rank_code[rank]);
       }
    }
}

int print_hand (suit)
{

}

int totrank_check (totrank)
{
    if (totrank > 21) {
        printf ("You lose!");
    }
    else if (totrank == 21) {
        printf ("You win!");
    }
}

int main()
{
     bool stay = {false};
     srand((unsigned) time(NULL));

     totrank = 0;
     num_cards = 2;
     printf("Your hand: ");
     while (num_cards > 0) {
         main_hand();
         totrank = totrank + (rank + 1);
     }
     printf("Total rank: %d\n", totrank);
     totrank_check(totrank);
     printf("\n");
     while (totrank < 24 && stay == false) {
        printf("Another card? 1. Yes 0. No\n");
        scanf("%d", &newcard);
        if(!newcard) {
            main_hand();
        }
        totrank = totrank + (rank + 1);
        totrank_check(totrank);
        printf("Total rank: %d\n", totrank);
        printf("Stay? 1. Yes 0. No\n");
        scanf("%d", &stay);
     }
    return 0;
}

Basically it's a code that "simulates" and hand of blackjack. 基本上,这是一个“模拟”二十一点的代码。 It starts, rand() chooses two numbers, the rank and the suit of the cards, that are set as true in a matrix so that they can't be chosen again in the same combination and then printed. 首先, rand()选择两个数字,即等级和纸牌花色,它们在矩阵中设置为true,因此无法再次以相同组合选择它们,然后进行打印。 There's a check for the total rank of the cards (so that if it exceeds 21 you automatically lose) and then you are asked if you want another card or you want to stay. 会检查卡的总等级(如果超过21,则会自动丢失),然后询问您是否要另一张卡或要留下。

Here's the error: if you choose that you want another cards, this new card will be the same as the last one. 这是错误消息:如果您选择要另外一张卡,那么这张新卡将与最后一张相同。 Basically, you get a Ace of Spades, the and Two of Diamonds, then you want another card and you get another Two of Diamonds. 基本上,您会得到一张黑桃A,钻石和两颗钻石,然后您想要另一张牌,而您会得到另一颗两颗钻石。 And the another, and another. 另一个,另一个。 If you remove the rank check in the second while you can see that the rank grows based on the rank of the last card. 如果您在第二秒移除排名检查,则可以看到排名根据最后一张卡的排名而增加。

Before, the printfs were in that print_hand() function, and you could see that you always got the same card, now I moved them in the main_hand() function because I thought that it might be the problem (it wasn't) and because having a separate function for the print was redundant. 以前,printfs在那个print_hand()函数中,您可以看到总是有相同的卡,现在我将它们移到main_hand()函数中,因为我认为这可能是问题所在(不是),因为具有单独的打印功能是多余的。 But you can see that technically, the if(!in_hand[suit][rank]) works, because, since the card is the same, it doesn't enter the if and it doesn't get printed. 但是从技术上您可以看到if(!in_hand[suit][rank])可以工作,因为由于卡相同,所以它不会输入if并且不会被打印。

I don't know what's causing this problem. 我不知道是什么引起了这个问题。 Any idea? 任何想法?

Please note I'm already using srand((unsigned) time(NULL)); 请注意,我已经在使用srand((unsigned) time(NULL)); to seed rand() . 播种rand()

scanf("%d", &newcard); and scanf("%d", &stay); scanf("%d", &stay); are a problem as the variables are bool , but the format specifier is for int . 由于变量是bool ,这是一个问题,但是格式说明符是用于int In 2 places, change like: 在2个地方,更改为:

// scanf("%d", &newcard);
int t;
scanf("%d", &t);
newcard = !!t;

3 functions return int but do not return anything. 3个函数返回int但不返回任何内容。 Change to void functions. 更改为void函数。

// int main_hand() {
void main_hand() {    

There appears to be other logic issues too. 似乎还有其他逻辑问题。
1) Code sometimes exists without knowing if won/loss 1)有时存在代码,而不知道赢/输
2) @Jongware comment above correct: 2)@Jongware上面的评论正确:

Lastly: If the same random sequence is still coming up, strip code to a bare main srand time printf rand and see if that works. 最后:如果仍然出现相同的随机序列,则将代码剥离到main srand time printf rand ,看看是否main srand time printf rand time() , if not working always returns -1. time() ,如果不起作用,则始终返回-1。
Or simple add the following and verify srand() called with different values. 或者简单地添加以下内容,并验证使用不同值调用的srand()

 int main(void) {
   unsigned now = (unsigned) time(NULL);
   printf("now = %u\n", now);
   srand(now);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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