robgeek
24th March 2017, 23:04
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.
//mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
File fle;
//...
//mainwindow.cpp
//...
void MainWindow::on_pushButton_clicked( ) {
fle.setLength( 5 );
fle.createFile( );
fle.showFile( );
}
#ifndef FILE_H
#define FILE_H
#define BS 4
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <QVector>
using namespace std;
class File {
public:
int length;
QVector<QVector<int> > file;
public:
File( );
void createFile( );
void setLength(int l);
void showFile( );
};
#endif // FILE_H
#include "file.h"
File::File( ) { }
void File::createFile( ) {
int limit = (BS + 1);
int x = (BS - 1);
//srand (time(NULL) ); // It works but only if i restart my program.
for (int i = 0; i < length; i++) {
QVector<int> byte( limit );
for (int j = x; j >= 0; j--) {
byte[j] = ( (rand( ) % 100 % 2) );
byte[BS] += (pow(2, (x - j)) * byte[j]);
}
file.push_back( byte );
}
}
void File::setLength(int l) {
length = l;
}
void File::showFile( ) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < (BS + 1); j++) {
cout << file[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
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
...
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.
//mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
File fle;
//...
//mainwindow.cpp
//...
void MainWindow::on_pushButton_clicked( ) {
fle.setLength( 5 );
fle.createFile( );
fle.showFile( );
}
#ifndef FILE_H
#define FILE_H
#define BS 4
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <QVector>
using namespace std;
class File {
public:
int length;
QVector<QVector<int> > file;
public:
File( );
void createFile( );
void setLength(int l);
void showFile( );
};
#endif // FILE_H
#include "file.h"
File::File( ) { }
void File::createFile( ) {
int limit = (BS + 1);
int x = (BS - 1);
//srand (time(NULL) ); // It works but only if i restart my program.
for (int i = 0; i < length; i++) {
QVector<int> byte( limit );
for (int j = x; j >= 0; j--) {
byte[j] = ( (rand( ) % 100 % 2) );
byte[BS] += (pow(2, (x - j)) * byte[j]);
}
file.push_back( byte );
}
}
void File::setLength(int l) {
length = l;
}
void File::showFile( ) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < (BS + 1); j++) {
cout << file[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
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
...