PDA

View Full Version : Problem with random numbers. Rand() not working as expected.



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

...

d_stranz
25th March 2017, 00:12
If you don't seed the random number generator (using srand()) it will always start at the same place and generate the same set of random numbers. I think you can understand that this is almost a necessity if you want to debug something that operates on a series of random numbers.

If you call srand() only once (or not at all) in your program, you will get the same sequence of numbers each time. If you want to get a different sequence, you must reseed the random number generator with a new seed using srand() in the button click slot or wherever you want to start a new random sequence.

It looks like the intent of your code is to produce a binary number of a given length. I'm not quite sure why you are computing the modulus of the random number first with 100 and then with 2 when simply using modulus 2 would do the same thing. Or you could just compute the logical AND of the random number with 1 to select the first bit and avoid the modulus arithmetic altogether. And you could easily compute the sum by simply shifting the current sum to the left by one bit and then ORing with the current bit each time through the loop instead of using exponentiation.

I don't see anywhere that you are initializing the "byte" vector to zero. This means that byte[BS] could have an indeterminate value at the start, so doing += arithmetic might result in nonsense. Use QVector< int > byte( limit, 0 ) to ensure that all elements in the vector are initialized to zero on construction.

robgeek
25th March 2017, 13:18
Thanks for your help d_stranz. I did what you said about bitwise operators.

I figured out my problem, but i don't know exactly why and how to solve this. The problem is i instanced the File object in the MainWindow class definition. The problem is this object has to be alive all the time(until i close my program).

d_stranz
26th March 2017, 00:20
So you can call srand() anywhere - it's just a C function. It doesn't have to be called anywhere in proximity to your use of rand() and has nothing to do with the scope or lifetime of your File object. If you are using a slot in MainWindow that responds to your button click, call it there and you'll re-seed the RNG on every click so should get a different series each time.