Getting around rand()'s limitations.

Say if you wanted to hash the value from 2 different calls to rand() at different seeds, what would be a simple and efficient way of doing this, and is it possible to only use <stdlib.h> to accoplish this?, without useing arrays or tables to store temporary data etc.

I know its possible to write your own random() class that has this capability, but the mathematics behind it is beyond me, so I appreciate anyone who can help me implement a function that uses rand() so that it will be able to seed, seed at a different value, and reseed at the point from which it left off, etc etc etc.

Say for example, as it is, the 3rd call to rand() when seeded with 0 is not the same as the first call to rand() when seeded with 3.

I want to be able to, for example, seed with time, take 10 calls to rand(), then seed with a different value, take 10 more calls to rand(), then take the 11th call of rand() had I not reseeded.

edit: The only way i know how to do this now is like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <stdlib.h>

int main()
{
    int start_time = time(NULL);
    int num = 0;
    srand(start_time);
    for (int x = 0; x < 10; ++x)
        {  
             num = rand();
             cout << num << endl;
        }
    int elapsed_time = (start_time - time(NULL));
    srand(elapsed_time);
    for (int y = 0; 0 < 10; ++y)
        {
              num = rand();
              cout << num << endl;
        }
    srand(start_time);
    for (int z = 0; z < 11; ++z)
        {//notice that this loop is recursive, which I do not want!!!!!!!!!
              rand();        
         }
    cout << rand() << endl;

    return 0;
}
Take a look at this, and also tell me what you think of it:
http://cplusplus.com/forum/lounge/23263/page7.html#msg126360

Watch how I manipulate multiple seeding inside my_improved_rand() function.
You can do it the same way with rand().

EDIT: You might also wanna take a look at my quiz above ;)
Last edited on
Topic archived. No new replies allowed.