mercredi 14 décembre 2016

Random number in a rang inside an Array

I'm trying to get random a random number between certain ranges in each column in an array so as to mimic a Bingo card.

The first column should contain numbers from 1 to 10, the second column numbers from 11 to 20, the third, 21 to 30 and so on up until the last column, which contains numbers from 81 to 90.

Here is my code:

for (row = 0; row<3; row++)
            {
                for (col = 0; col<9; col++)
                {
                    if (col == 0)
                    {
                        bingoCard[row][col] = rand() % ((10 + 1) - 1) + 1;
                    }//end of 1st col

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

                    }//end of 2nd col

                    else if (col == 2)
                    {
                        bingoCard[row][col] = rand() % ((30 + 1) - 20) + 20;

                    }//end of 3rd col

                    else if (col == 3)
                    {
                        bingoCard[row][col] = rand() % ((40 + 1) - 30) + 30;

                    }//end of 4th col

                    else if (col == 4)
                    {
                        bingoCard[row][col] = rand() % ((50 + 1) -40) + 40;

                    }//end of 5th col

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

                    }//end of 6th col

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

                    }//end of 7th col

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

                    }//end of 8th col

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

                    }//end of 9th col

                }// end col for

                printf("\n");

            }// end row for 

            for (row = 0; row < 3; row++)
            {
                for (col = 0; col < 9; col++)
                {
                    printf("%d \t", bingoCard[row][col]);

                }// end col for

                printf("\n");

            }// end row for ptinting the array

The code outputs the following :

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

Player : 1 's card



2       19      29      5       17      27      8       12      27
5       17      27      8       12      27      37      48      81
8       12      27      37      48      81      68      140     120

What am I doing that the range becomes wrong after the 3rd column?

Is there a better way to do this so that the numbers are random every time I run the program?




Aucun commentaire:

Enregistrer un commentaire