user3741346
user3741346

Reputation: 31

Sequences of random numbers output by program do not match with actual rand() output on Linux (works correctly on Windows)

Hopefully I'm allowed to ask this again now that my closed question got automatically deleted?

So, this is what I'm trying to do:

The program checks sequences of numbers output by rand() % 10 for certain strings of numbers. In this case the strings 444 and 555. So, on Windows, the first sequence that the program finds which contain those strings (looking at only the first 10 numbers), is the sequence output by seed 61163. The sequence is 3355444555.

Now, just to make sure, I check what rand() % 10 actually outputs with that seed with a simple program (both programs shown below), which is 3355444555. Very much expected.

So far everything makes sense.

On Linux however, the output of the program does not match with what rand() actually outputs. So for example, the first sequence that the program finds containing strings 444 and 555 in the first 10 numbers, is the sequence output by seed 154950. The sequence is 4555232444.

Still everything makes sense (rand() has different implementations on Windows/Linux so it makes sense that the first found sequence is completely different than on Windows).

However, and this is the problem: When checking what rand() % 10 actually outputs with that seed, unlike on Windows, the sequences do not match. The sequence that seed 154950 outputs is actually 1778785265. (tested with the same simple program).

And that is my problem. As mentioned before the really weird part is that the code works on Windows, and I'm not sure how anything Linux-specific could screw this up.

The code for the program:

#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;

int main()
{   
    int result;
    string text;    
    int minSeed;
    int maxSeed;
    string temp;
    cout << "Seed Seeker. Checks a range of random seeds for specific strings.\n";
    cout << "Random Seed: lowest value: ";
    getline(cin, temp);
    minSeed = atoi(temp.c_str());
    cout << "Random Seed: highest value: ";
    getline(cin, temp);
    maxSeed = atoi(temp.c_str());
    cout << "Generate how many random numbers / seed? ";
    getline(cin, temp);
    int rndRange = atoi(temp.c_str());
    string lookForThisA="444";
    string lookForThisB="555";

    for (int j = minSeed; j <= maxSeed; ++j)
    {
        text = "";
        for (int i = 0; i < rndRange; ++i){
            result = rand() % 10;
            text += static_cast<ostringstream*>(&(ostringstream() << result))->str();
        }
        if (text.find(lookForThisA) != std::string::npos) {
            if (text.find(lookForThisB) != std::string::npos) {
                std::cout << "\n\n\nString found!!!!\n\n";
                cout << text << "\n\nSeed: " << j << " ";
            }
        }
    }
} 

Program used to check rand() output:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    srand(154950);
    cout << rand() % 10;
    cout << rand() % 10;
    cout << rand() % 10;
    cout << rand() % 10;
    cout << rand() % 10;
    cout << rand() % 10;
    cout << rand() % 10;
    cout << rand() % 10;
    cout << rand() % 10;
    cout << rand() % 10;
} 

Upvotes: 3

Views: 157

Answers (1)

YePhIcK
YePhIcK

Reputation: 5856

Make sure you seed the RNG every loop iteration:

for (int j = minSeed; j <= maxSeed; ++j)
{
    srand(j);  // <----- without that the RNG is not re-seeded
    text = "";

As for Windows vs. Linux my guess is that on Windows (I assume Debug build) the srand was called for you by CRT and the 61163 is not a seed in this case but just a number of times the 10-digit loop iterated before getting to your sequence ;-)

Upvotes: 1

Related Questions