+ 2

Sudoku Challenge Help

So I have a working validator here: https://code.sololearn.com/c95kvF53HSZ5/?ref=app I want to be able to fill the grid with values from 1 to 9 randomly, pretty much each number will have a random location in the vector and will not repeat. I can't seem to get it working, my other attempts were in separate unsaved codes so I'd have to re-write to show you. But how can I do it?

27th Mar 2019, 3:44 PM
Daniel Cooper
Daniel Cooper - avatar
2 Answers
+ 2
To start off, I haven't touched any C++ program in literal months, so my solution is probably as janky as it can be. What I did was create 3 new variables - arr, c, and count. arr will be an array consisting of any numbers that have already been used, c will be a number set to random (from 1 - 9), and count will be a counter that allows arr to be populated with the values. From there I changed up the inner for loop within fillGrid() function so that rather than having b be the number getting pushed to the vector c gets pushed instead (the while loop is used to make sure that the value of c is not repeated). https://code.sololearn.com/c4iupx0FdnS3/?ref=app
27th Mar 2019, 5:07 PM
Faisal
Faisal - avatar
+ 1
Try using the rand() function. Example in C -   int i;  // Use current time as    // seed for random generator     srand(time(0));     for (i = 0; i < count; i++) {    int num = (rand() % (upper - lower + 1)) + lower;    printf("%d ", num);     } // upper =10 lower =0 for Sudoku Hope this helps...!!!       
27th Mar 2019, 5:11 PM
Kuri
Kuri - avatar