使用循环在数组中生成唯一随机数

所以问题是开发一个[5] [5]表,每个表包含1-100的唯一数字( 没有重复

所以这就是我提出的:

#include  #include  #include  int main() { int outerLoop; int innerLoop; int board[5][5]; /* Array Name And Size*/ /* seeds the random number generator*/ srand(time(NULL)); int number; number = rand() % 101; /* Start a loop to generate a random integer between 1 and 100 and assign it into the array. The loop runs 25 times*/ for ( outerLoop = 0 ; outerLoop <= 25 ; outerLoop++ ) /* loop 25 times*/ { for ( innerLoop = 0 ; innerLoop <= 4 ; innerLoop++ ) /* <=4 due to 5 columns*/ { board[outerLoop][innerLoop] = rand() % 100 + 1; } printf( "%d \n", board[outerLoop][innerLoop] ); } 

所以我几乎被困在这里。我不太确定这个:

 board[outerLoop][innerLoop] = rand() % 100 + 1; 

我简单地说:/任何想法的人?

你想要的是一个随机算法

C中的随机数组

使你的25个元素的独特#s数组从1到100; 只需创建一个数字为1..100的100个元素数组,从100个游泳池中移动第一个25,然后使用第一个25。

 $ cat test.c #include  #include  void shuffle(int *array, size_t array_size, size_t shuff_size) { if (array_size > 1) { size_t i; for (i = 0; i < shuff_size - 1; i++) { size_t j = i + rand() / (RAND_MAX / (array_size - i) + 1); int t = array[j]; array[j] = array[i]; array[i] = t; } } } int main(int argc, char * argv[]) { int a[100]; int b[5][5]; int i,j,k=0; for(i=0; i<100;++i) a[i]=i; shuffle(a,100,25); for(i=0;i<5;++i) for(j=0;j<5;++j) { b[i][j] = a[k++]; printf("%d ",b[i][j]); } printf("\n"); } $ gcc -o test test.c $ ./test 0 14 76 47 55 25 10 70 7 94 44 57 85 16 18 60 72 17 49 24 53 75 67 9 19 

把它想象成一张100张牌。

  • 创建一个包含卡号的100个元素数组(1..100)
  • 随机播放arrays(= deck)。 (参见@ koodawg的回答和@ Steve314的评论)
  • 将自己的甲板上的前25张牌“交易”成5x5arrays。

只需创建大小为100的布尔数组: bool numberUsed[100] 。 然后在周期中:

 1.Generate random number r 2.If numberUsed[r] is true, dont add that r anywhere and continue in loop 3.numberUsed[r] = true 

请注意,您需要使用此方法的WHILE循环,而不是FOR循环。

这是一些解决它的伪代码:

  • 创建一个长度为100的“列表”,其中包含数字1 … 100,称为“numbersAvailable”
  • 在你的内部循环中设置index = (int)rand() * numbersAvailable; 并取数字numbersAvailable.get(index); 然后执行numbersAvailable.remove(index);

在Java中创建列表很容易。 如果你想坚持C,你必须通过数组模拟这个。 (我可以写下解决方案,但这看起来像是一个家庭作业,所以我留给你一些东西)。

注意:与试验和拒绝解决方案相比,此解决方案具有构建结果所需的固定时间量的优势。

int board[5][5]; 分配一个连续的内存量,你可以用它初始化它

 for (i = 0; i < sizeof(board)/sizeof(int); i++) board[0][i] = rand() % 100 + 1; 

或者像你一样使用双循环,但是你只需要在另一个循环中循环5次,或者使用sizeof自动设置迭代次数:

 for ( outerLoop = 0 ; outerLoop < sizeof(board)/sizeof(board[0]) ; outerLoop++ ) { for ( innerLoop = 0 ; innerLoop < sizeof(board[0])/sizeof(board[0][0]) ; innerLoop++ ) { board[outerLoop][innerLoop] = rand() % 100 + 1; } } 

请记住,只有在编译时知道数组的长度时, sizeof才会以这种方式在数组上工作,就像在您的示例中一样。

C以行主顺序存储数组,即第0行的元素首先出现,然后是第1行的元素,依此类推。

在此处输入图像描述
我们可以通过查看int board[5][5]作为int board[5*5]来利用这一点。

 #include  #include  #include  #define N 5 int main() { int i, outerLoop = 1; int board[N*N]; srand(time(NULL)); int number; board[0] = rand() % 100 + 1; //initializing the first element while(1) { number = rand() % 100 + 1 ; if(outerLoop == N*N) break; else { //Cheking the previous elements for no duplicacy for ( i = 0; i < outerLoop; i++) { if(number == board[i]) break; } //confirming whether all the elements are checked or not and the assigning number to the array element and then increment the counter outerLoop if(i == outerLoop) { board[outerLoop] = number; outerLoop++; } else continue; } } //Printing the elements of array board[N*N] for ( outerLoop = 0 ; outerLoop < N*N ; outerLoop++ ) { printf( "%d\t", board[outerLoop] ); if(outerLoop % N == 4) printf("\n\n"); } }