3

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;
} 
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 3
    As far as I'm aware, the standard doesn't mandate a specific implementation of rand; only that it will produce the same sequence of numbers for the same seed. So it's up to the compiler / operating system which specific sequence you will get. See this question: http://stackoverflow.com/questions/1026327/what-common-algorithms-are-used-for-cs-rand. In short, you cannot expect the seeds to be portable between platforms, or even between compilers. – Dave Jul 01 '14 at 19:54
  • 3
    Sorry, I can't spot any call of seeding (`srand(int)`) the random engine in your 1st sample at all?!? Those programs you mentioned are unrelated? – πάντα ῥεῖ Jul 01 '14 at 19:57
  • Also for future reference; if one of your questions gets closed you are *encouraged* to edit it to try to make it clearer. Questions can be reopened if the community thinks they have been brought up-to-scratch. Though I can't comment on specifics since I didn't see your original question or the close reason. – Dave Jul 01 '14 at 19:58

1 Answers1

1

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

YePhIcK
  • 5,816
  • 2
  • 27
  • 52