Hi!

I'm trying to create a QVector<QVector<int>> with random numbers when i click on a QButton but i get always the same set of numbers. How can i solve this problem? Even restarting my program, i get always the same numbers. Using "srand (time(NULL) )" works fine but only if i restart the program.

Qt Code:
  1. //mainwindow.h
  2. class MainWindow : public QMainWindow
  3. {
  4. Q_OBJECT
  5.  
  6. public:
  7. File fle;
  8. //...
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //mainwindow.cpp
  2. //...
  3. void MainWindow::on_pushButton_clicked( ) {
  4. fle.setLength( 5 );
  5. fle.createFile( );
  6. fle.showFile( );
  7. }
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #ifndef FILE_H
  2. #define FILE_H
  3.  
  4. #define BS 4
  5.  
  6. #include <iostream>
  7. #include <math.h>
  8. #include <stdlib.h>
  9.  
  10. #include <QVector>
  11.  
  12. using namespace std;
  13.  
  14. class File {
  15. public:
  16. int length;
  17. QVector<QVector<int> > file;
  18.  
  19. public:
  20. File( );
  21.  
  22. void createFile( );
  23. void setLength(int l);
  24. void showFile( );
  25. };
  26.  
  27. #endif // FILE_H
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #include "file.h"
  2.  
  3. File::File( ) { }
  4.  
  5. void File::createFile( ) {
  6. int limit = (BS + 1);
  7. int x = (BS - 1);
  8. //srand (time(NULL) ); // It works but only if i restart my program.
  9.  
  10. for (int i = 0; i < length; i++) {
  11. QVector<int> byte( limit );
  12.  
  13. for (int j = x; j >= 0; j--) {
  14. byte[j] = ( (rand( ) % 100 % 2) );
  15. byte[BS] += (pow(2, (x - j)) * byte[j]);
  16. }
  17.  
  18. file.push_back( byte );
  19. }
  20. }
  21.  
  22. void File::setLength(int l) {
  23. length = l;
  24. }
  25.  
  26. void File::showFile( ) {
  27. for (int i = 0; i < length; i++) {
  28. for (int j = 0; j < (BS + 1); j++) {
  29. cout << file[i][j] << " ";
  30. }
  31. cout << endl;
  32. }
  33.  
  34. cout << endl;
  35. }
To copy to clipboard, switch view to plain text mode 

Output: No matter how many times I restart my program or click on the button.
1 1 0 1 13
0 0 1 1 3
1 0 1 1 11
0 1 1 0 6
0 0 0 0 0

1 1 0 1 13
0 0 1 1 3
1 0 1 1 11
0 1 1 0 6
0 0 0 0 0

...