#include "pch.h" #include "stdio.h" #include "stdlib.h" #include "time.h" int m[9] = {0};int main (void) {Srand (time (0)); for (int i = 1; i < ten; i++) {M[i-1] = rand ()%9+1;for (int j = 0; J < I-1, J + +) {while (m[j] = = M[i-1]) {m[i-1] = rand ()% 9 + 1;}} for (int i = 0; i < 9; i++) {printf ("%d", M[i]);}}
The general idea is no problem, produce a random number, we assume it at M[i-1] position, then need from m[0] to m[i-2] and m[i-1] in order to determine whether the random number has been generated before.
while (m[j] = = M[i-1]) {m[i-1] = rand ()% 9 + 1;}
However, this paragraph is problematic, when encountering a random number that has already been generated before, and then produce a new random number, and at this time M[j] J value may not start from 0, so m[j] from the current value of J to M[i-2], and less m[0]~m[j-1] judgment, which is the root cause of the bug.
Random generation with Bugs does not repeat 1-9 of the code