As the title says, i'm trying to randomly generate Sudoku (in this example 4x4) and i know how to do it, kinda, i believe it should work, but for some reason my program crashes when i run it ... Sometimes it works and when it does Sudoku is then spawned like this :

sudokuWorking.PNG
https://imgur.com/Ctf0046

But when it doesn't work, which is like 70% of the time, then it crashes and its like this :

sudokuCrash.jpg
https://imgur.com/2ksNiMQ

I put qDebug() in my while loop and i realized that my loop keeps repeating itself and idk why....
In Igrica.h file i defined stiriRandom as int table of [4][4]. When i press 4x4 button, size is set to 4.

Qt Code:
  1. void Igrica::randomTabela(){
  2.  
  3. int random_number;
  4. qDebug() << "size" << size;
  5. if(size == 4){
  6. qDebug() << "hi";
  7. for(int i=0;i<size;i++){
  8. for(int k=0;k<size;k++){
  9.  
  10. bool help = false;
  11. while(help == false){
  12. qDebug() << "um stuck here";
  13. random_number = (rand()%4)+1;
  14.  
  15. // checking if there is the same number as random_number in a row
  16. for(int j=0;j<size;j++){
  17. if(stiriRandom[j][k] == random_number){
  18. help = false;
  19. break;
  20. }else{
  21. help = true;
  22. }
  23. }
  24. // checking is there is another random_number in collumn
  25. if(help == true){
  26. for(int j=0;j<size;j++){
  27. if(stiriRandom[i][j] == random_number){
  28. help = false;
  29. break;
  30. }else{
  31. help = true;
  32. }
  33. }
  34. }
  35.  
  36. if(help == true)
  37. stiriRandom[i][k] = random_number;
  38. }
  39. }
  40. }
  41. }else if(size == 9){
  42. qDebug() << "hi";
  43. for(int i=0;i<size;i++){
  44. for(int k=0;k<size;k++){
  45.  
  46. bool help = false;
  47. while(help == false){
  48.  
  49. random_number = rand()%9+1;
  50.  
  51. for(int j=0;j<size;j++){
  52. if(devetRandom[j][k] == random_number){
  53. help = false;
  54. break;
  55. }else{
  56. help = true;
  57. }
  58. }
  59.  
  60. if(help == true){
  61. for(int j=0;j<size;j++){
  62. if(devetRandom[i][j] == random_number){
  63. help = false;
  64. break;
  65. }else{
  66. help = true;
  67. }
  68. }
  69. }
  70.  
  71. if(help == true)
  72. devetRandom[i][k] = random_number;
  73. }
  74. }
  75. }
  76. }else if(size == 12){
  77. for(int i=0;i<size;i++){
  78. for(int k=0;k<size;k++){
  79.  
  80. }
  81. }
  82. }
  83. }
To copy to clipboard, switch view to plain text mode 

Igrica header file

Qt Code:
  1. #ifndef IGRICA_H
  2. #define IGRICA_H
  3.  
  4. #include <QGraphicsView>
  5. #include <QGraphicsScene>
  6. #include <QObject>
  7. #include <QMouseEvent>
  8. #include <QGraphicsSceneMouseEvent>
  9. #include <QGraphicsScene>
  10. #include <QWidget>
  11. #include <QKeyEvent>
  12.  
  13. #include "polje.h"
  14. #include "igralec.h"
  15.  
  16. class Igrica: public QGraphicsView{
  17. Q_OBJECT
  18. public:
  19. Igrica(QWidget *parent=NULL);
  20. Igralec *igralec;
  21. void meni();
  22. void zapolniTabelo();
  23. void izpisiTabelo();
  24. void randomTabela();
  25. void narisiOdebeljeneCrte();
  26. void naredimoFinishButton();
  27. Polje *stiri[4][4];
  28. Polje *devet[9][9];
  29. Polje *dvanajst[12][12];
  30. int stiriRandom[4][4];
  31. int devetRandom[9][9];
  32. int dvanajstRandom[12][12];
  33. public slots:
  34. void start();
  35. void meniFour();
  36. void meniNine();
  37. void meniTwelve();
  38. void preveriZmago();
  39.  
  40. private:
  41. int size;
  42. int level;
  43. };
  44.  
  45. #endif // IGRICA_H
To copy to clipboard, switch view to plain text mode 

Any help is appreciated !!!!!!!!!!!!!!!!!!!!!!