jeudi 11 juillet 2019

How to find the next seed that keeps the sequence of random numbers going in C?

I have an assignment in which I have to program a M/M/1 queue with random arrivals and service times. My problem is this: My professor wants us to prove that the queue is working properly and generating the correct statistics (Wait time, Avg number of people in queue, etc) regardless of the seed that is used to initialize the random number generator.

That means he wants us to use seeds that will not generate sequences that overlap. For example: If I initialize the random number generator with seed X and it generates the sequence [3, 5, 9, 2, 0], the next seed Y cannot contain (and continue) this same sequence. One suggestion he did give us was to simulate an entire run of the queue and, at the end, find a seed that will continue the sequence from the number it stopped. Lets say our run with seed X generated the same sequence as above and the next call to rand() would have returned the number 19. He wants us to find a seed Y that will generate 19 as its first random number and then continue from there since these sequences will not overlap

As another suggestion he mentioned that if you used the last number generated by rand() in your previous run as the seed for your next run, the first number would be the continuation of the previous sequence, but I was unable to reproduce that. I have written the following code as a test to see if this method works:

//Initialize the random number generator
srand(10);

int new_seed;

//Generate a small test sequence of random numbers
for(int i = 0; i < 5; i++){
    //Store the new seed for later use
    if(i == 3){
        new_seed = rand();
        printf("%d: %d -> new seed!\n",i, new_seed);
    }else{
        printf("%d: %d\n",i, rand());
    }
}

printf("\n");
//Reinitialize the number generator with the new seed
srand(new_seed);

//Generate another small test sequence of random numbers
for(int i = 0; i < 5; i++){
    printf("%d: %d\n",i, rand());
}


The code above produces the following output:

0: 1215069295
1: 1311962008
2: 1086128678
3: 385788725 -> new seed!
4: 1753820418

0: 1008718395
1: 1489456950
2: 1335983464
3: 366125540
4: 20514025

Which is not what I was expecting. As per my professor's explanation, the second sequence should have started with the number 1753820418, as it was the last one of the previous sequence. Is this correct? Is there another way of finding out which seed will continue the sequence of the previous seed in C?

Thank you very much for the help!




Aucun commentaire:

Enregistrer un commentaire