您現在的位置是:首頁 > 棋牌

如何使用隨機數實現自動發撲克牌?

  • 由 一口Linux 發表于 棋牌
  • 2022-08-10
簡介總結一下功能點:rand()回產生1個隨機的整型數,介於[0~RAND_MAX]之間srand設定產生一系列偽隨機數發生器的起始點,要想把發生器重新初始化,可用1作seed值

打橋牌幾個人

學習不止! 問答不止!

一、粉絲問題

如何使用隨機數實現自動發撲克牌?

二、相關函式說明

1。 函式說明

產生隨機數的方法很多,常用的是rand()、srand(),來看一下這2個函式的定義:

SYNOPSIS       #include        int rand(void);       int rand_r(unsigned int *seedp);       void srand(unsigned int seed);

DESCRIPTION       The  rand()  function returns a pseudo-random integer in the range 0 to       RAND_MAX inclusive (i。e。, the mathematical range [0, RAND_MAX])。       The srand() function sets its argument as the seed for a  new  sequence       of  pseudo-random  integers  to be returned by rand()。  These sequences       are repeatable by calling srand() with the same seed value。       If no seed value is provided,  the  rand()  function  is  automatically       seeded with a value of 1。RETURN VALUE       The rand() and rand_r() functions return a value between 0 and RAND_MAX       (inclusive)。  The srand() function returns no value。

ubuntu的man手冊都是英文的,建議各位同學把自己的英文水平好好提升一下,熟練的閱讀英文技術文件一名合格的碼農必備技能。

總結一下功能點:

rand()回產生1個隨機的整型數,介於[0~RAND_MAX]之間

srand設定產生一系列偽隨機數發生器的起始點,要想把發生器重新初始化,可用1作seed值。任何其它的值都把發生器匿成一個隨機的起始點。rand檢索生成的偽隨機數。在任何呼叫srand之前呼叫rand與以1作為seed呼叫srand產生相同的序列。

srand僅用於設定偽隨機數發生器的起始點,並不產生隨機數

2。 函式例項

為了說明這幾個函式的使用特性,我們舉下面幾個例子。

1) rand

1 #include   2 #include   3   4 int main(int argc, char **argv)  5 {  6     int i;  7   8     for(i=0;i<10;i++)  9     { 10         printf(“%d ”,rand()%100); 11     } 12     putchar(‘\n’);                                                                                          13     return 0; 14 }

程式碼很簡單,產生一個隨機數,然後對100取餘,

編譯執行

如何使用隨機數實現自動發撲克牌?

從結果可以看出,從單次來看,每次產生的隨機數是隨機的,但是每次執行,隨機佇列是一致的,

所以此時還達不到我們真正的產生隨機數的要求。

2) srand

修改程式碼如下:

1 #include   2 #include   3 #include   4   5 int main(int argc, char **argv)  6 {  7     int i;  8   9  10     srand((unsigned)time(NULL));                                          11     for(i=0;i<10;i++) 12     { 13         printf(“%d ”,rand()%100); 14     } 15     putchar(‘\n’); 16     return 0; 17 }

如何使用隨機數實現自動發撲克牌?

從結果來看,每次產生的數列基本保持隨機,但是如果輸入速度太快,1秒之內,仍會產生相同的序列。 這個一定要注意

三、最終實現

我們把問題相對最佳化一下: 隨機產生0-99之間的數,統計出出現次數最多的那個數。

/*   *公眾號:一口Linux *2021。6。21 *version: 1。0。0*/#include #include #include #include #define _MAX_ARRAY 100int main(int argc, char **argv){ int i; int num,max,index; int count[_MAX_ARRAY];  memset(count,0,sizeof(count));  srand((unsigned)time(NULL));  for(i=0;i<10000;i++) {  num = rand()%100;  count[num]++; } //find max  max = count[0]; index = 0; for(i=1;i<_MAX_ARRAY;i++) {  if(max < count[i])  {   max = count[i];   index = i;  } } printf(“max is count[%d],appear %d times\n”,index,count[index]); return 0;}

執行結果如下:

如何使用隨機數實現自動發撲克牌?

四、如何實現自動發牌的遊戲?

一副撲克有52張牌,打橋牌時應將牌分給4個人。請設計一個程式完成自動發牌的工作。要求:黑桃用S (Spaces)表示,紅桃用H (Hearts)表示,方塊用D (Diamonds)表示,梅花用C (Clubs)表示。

如何使用隨機數實現自動發撲克牌?

1。 分析:

要設定陣列表現撲克牌 要設定陣列表現玩家 要給撲克牌做特定標識,得到結果後玩家要知道自己手中黑桃有哪些、方塊有哪些

2。 設計思路:

設定4個字元陣列儲存4種梅花牌,設定4個字元陣列表示4名玩家分配到的牌 每張牌隨機發給4名玩家,當玩家的持牌數達到13,不再分配給該名玩家牌

3。 原始碼

/*   *公眾號:一口Linux *2021。6。21 *version: 1。0。0*/#include #include #include #include void poke_13(){ /*全部牌*/ char S[13] = { ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘T’, ‘J’, ‘Q’, ‘K’, ‘A’ }; char H[13] = { ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘T’, ‘J’, ‘Q’, ‘K’, ‘A’ }; char D[13] = { ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘T’, ‘J’, ‘Q’, ‘K’, ‘A’ }; char C[13] = { ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘T’, ‘J’, ‘Q’, ‘K’, ‘A’ }; /*4個玩家*/ char player1[13], player2[13], player3[13], player4[13]; int p1 = 0, p2 = 0, p3 = 0, p4 = 0; srand((unsigned)(time(NULL))); distribution(S, player1, player2, player3, player4, &p1, &p2, &p3, &p4); distribution(H, player1, player2, player3, player4, &p1, &p2, &p3, &p4); distribution(D, player1, player2, player3, player4, &p1, &p2, &p3, &p4); distribution(C, player1, player2, player3, player4, &p1, &p2, &p3, &p4); puts(“發牌結果”);  puts(“play1:”); for (int i = 0; i < 13; i++)  printf(“%c ”, player1[i]); putchar(‘\n’);  puts(“play2:”);  for (int i = 0; i < 13; i++)  printf(“%c ”, player2[i]); putchar(‘\n’);  puts(“play3:”); for (int i = 0; i < 13; i++)  printf(“%c ”, player3[i]); putchar(‘\n’); puts(“play4:”); for (int i = 0; i < 13; i++)  printf(“%c ”, player4[i]); putchar(‘\n’);}void distribution(char * S_H_D_C, char * player1, char * player2, char * player3, char * player4, int *p1, int *p2, int *p3, int *p4){ static int h = 1; int r; int a = *p1, b = *p2, c = *p3, d = *p4; for (int i = 0; i < 13; i++) {  r = (rand() % 4) + 1;  //保證了當某個人得到13張牌後不在得牌  while ((r == 1 && (*p1) == 13) || (r == 2 && (*p2) == 13) || (r == 3 && (*p3) == 13) || (r == 4 && (*p4) == 13))   r = (rand() % 4) + 1;  switch (r)  {   case 1:    player1[(*p1)++] = S_H_D_C[i];    break;   case 2:    player2[(*p2)++] = S_H_D_C[i];    break;   case 3:    player3[(*p3)++] = S_H_D_C[i];    break;   case 4:    player4[(*p4)++] = S_H_D_C[i];    break;   default:    break;  } } switch (h++) {  case 1:   printf(“黑桃:\n”);   break;  case 2:   printf(“紅桃:\n”);   break;  case 3:   printf(“方塊:\n”);   break;  case 4:   printf(“梅花:\n”);   break; } printf(“Player1:”); for (int i = a; i < (*p1); i++)  printf(“%c ”, player1[i]); putchar(‘\n’); printf(“Player2:”); for (int i = b; i < (*p2); i++)  printf(“%c ”, player2[i]); putchar(‘\n’); printf(“Player3:”); for (int i = c; i < (*p3); i++)  printf(“%c ”, player3[i]); putchar(‘\n’); printf(“Player4:”); for (int i = d; i < (*p4); i++)  printf(“%c ”, player4[i]); putchar(‘\n’);}int main(int argc, char **argv){ poke_13(); return 0;}

執行結果如下:

如何使用隨機數實現自動發撲克牌?

這真是一個demo,實際上我們手機上很多的紙牌遊戲,發牌演算法要複雜的多,但是萬變不離其宗,思想都是一樣的。

還等什麼呢?快來試試吧!

程式碼來源於網路,侵權刪。

Top