Ich versuche, eine zufällige Zahl zwischen bestimmten Bereichen in jeder Spalte in einem Array zu erhalten, um eine Bingokarte nachzuahmen.Zufallszahl in einem Bereich innerhalb eines Arrays
sollten die ersten Spaltennummern enthalten 1 bis 10, die zweite Spalte Zahlen von 11 bis 20, die dritten, 21 bis 30 und so weiter bis zur letzten Kolonne, die Zahlen von 81 bis 90.
enthältHier ist meine überarbeitete Code basiert auf Antworten unter:
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
der Code gibt nun die folgenden:
***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
noch weg es ist viel, wenn es vorbei am 3. colum geht n? Von dort ist ein Wert in jeder Spalte richtig?
Was ist 'rang'? –
@SouravGhosh bedeutet er wahrscheinlich Bereich – Adalcar
1) Jeder Bereich ist 10 breit, also sollten alle "% 10" oder gleichwertig sein, aber "col == 1" zu "col == 4" verwenden "11". /// 2) Die Bereiche sind sogar wonkier für 'col> = 5'. z.B. 'rand()% (60 + 1) + 50 'erzeugt eine Zahl in' [50..111] '. /// 3) Ich denke, Sie haben eine andere Version des Codes ausgeführt als die, die Sie gepostet haben. Ich sehe nicht, wie 'rand()% ((40 + 1) - 30) + 30 '" 5 "erzeugen könnte (da Sie" 30 "zu einer nicht negativen Zahl hinzufügen). – ikegami