Results 1 to 4 of 4

Thread: Problem with random numbers. Rand() not working as expected.

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Problem with random numbers. Rand() not working as expected.

    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

    ...

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with random numbers. Rand() not working as expected.

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    robgeek (25th March 2017)

  4. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Problem with random numbers. Rand() not working as expected.

    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).

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with random numbers. Rand() not working as expected.

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. random numbers and slots
    By Enoctaffie in forum Newbie
    Replies: 6
    Last Post: 14th April 2012, 07:54
  2. QDir not working as expected
    By BettaUseYoNikes in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2011, 16:55
  3. Replies: 7
    Last Post: 22nd November 2010, 18:35
  4. Replies: 1
    Last Post: 7th April 2010, 16:26
  5. Qt large random numbers
    By timmu in forum Qt Programming
    Replies: 11
    Last Post: 31st August 2009, 08:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.