2016-12-14 16 views
-2

Bingoカードを模倣するために、配列の各列の特定の範囲の間に乱数をランダムに取得しようとしています。配列内の範囲内の乱数

最初の列には21〜30というように最大90

に81から番号が含まれている最後の列まで、第三の、20まで、10まで11から第二の列番号を1から番号を含まなければなりません

for (row = 0; row<3; row++) 
      { 
       for (col = 0; col<9; col++) 
       { 
        if (col == 0) 
        { 
         bingoCard[row][col] = (rand() % 10) + 1 + col * 10; 

        }//end of 1st col 

        else if (col == 1) 
        { 
         bingoCard[row][col] = (rand() % 10) + 1 + col * 10; 

        }//end of 2nd col 

        else if (col == 2) 
        { 
         bingoCard[row][col] = (rand() % 10) + 1 + col * 10; 

        }//end of 3rd col 

        else if (col == 3) 
        { 
         bingoCard[row][col] = (rand() % 10) + 1 + col * 10; 

        }//end of 4th col 

        else if (col == 4) 
        { 
         bingoCard[row][col] = (rand() % 10) + 1 + col * 10; 

        }//end of 5th col 

        else if (col == 5) 
        { 
         bingoCard[row][col] = (rand() % 10) + 1 + col * 10; 

        }//end of 6th col 

        else if (col == 6) 
        { 
         bingoCard[row][col] = (rand() % 10) + 1 + col * 10; 

        }//end of 7th col 

        else if (col == 7) 
        { 
         bingoCard[row][col] = (rand() % 10) + 1 + col * 10; 

        }//end of 8th col 

        else if (col == 8) 
        { 
         bingoCard[row][col] = (rand() % 10) + 1 + col * 10; 

        }//end of 9th col 

       }// end col for 

コード今、次を出力します:

***New Game*** 
How many players? 
1 

Player : 1 's card 



2  18  25  5  16  26  8  17  22 
5  16  26  8  17  22  35  43  54 
8  17  22  35  43  54  63  73  82 

それは第三colum過ぎて行く一度は道まだオフです。ここ

は、以下の回答に基づいて、私の改訂版のコードがありますn?そこから各列の1つの値が正しいでしょうか?

+3

'rang'は何ですか? –

+1

@SouravGhosh彼はおそらく範囲 – Adalcar

+0

を意味する1)各範囲は10幅なので、すべてが '%10'かそれと同等でなければならないが、 'col == 1'から 'col == 4 'までは' 11'を使う。 /// 2) 'col> = 5'の範囲はさらに丈夫です。例えば'rand()%(60 + 1)+ 50'は' [50..111] 'に数字を生成します。 /// 3)投稿したコードとは異なるバージョンのコードを実行したと思います。私は 'rand()%((40 + 1) - 30)+ 30'が' 5'を生成する方法を見ていない(非負の数値に '30 'を追加しているので)。 – ikegami

答えて

0

実際には、0〜9の間の乱数しか生成せず、各列の「ベース」を追加しています。ここに完全な例があります。

#include <stddef.h> 

#define ROWS 3 
#define COLS 9 
int bingoCard[ROWS][COLS]; 

main() 
{ 
    int col, row; 

    srand(time(NULL)); 

    for (row = 0; row<ROWS; row++) 
    { 
     for (col = 0; col<COLS; col++) 
     { 
      bingoCard[row][col] = (rand() % 10) + 1 + col*10; 
     } 
    } 

    for (row = 0; row<ROWS; row++) 
    { 
     for (col = 0; col<COLS; col++) 
     { 
      printf("%d\t", bingoCard[row][col]);   
     } 
     /* put a newline */ 
     puts("");  
    } 
} 
+0

私のコードをあなたの提案で更新しましたが、ここではっきりわからないものがありますか? – ICM89

+0

@ ICM89より完全な例があります。 – cleblanc

1

これは、範囲内の乱数を取得する非常に簡単な作業

を実行する複雑な手段であるが(rand() % range_size) + range_startの単純な問題です。

ここ

for (col = 0; col < 9; col++) 
{ 
    for (row = 0; row < 3; row++) 
    { 
     bingoCard[row][col] = rand() % 10 + (col * 10 + 1); 
    } 
} 
+0

'RAND_MAX'を呼び出す前に、' srand(time(NULL)); 'を実行することで、' RAND_MAX'を考慮したいと思うでしょう。 – alk

関連する問題