Use of Initstate and setstate for random number gen. on Unix 4.2 BSD
Vijay Chauhan
chauhan at UDEL-DEWEY.ARPA
Wed May 1 04:43:00 AEST 1985
I have a problem using the "initstate" and "setstate" procedures provided
as part of "random"(not "rand")-the random number generator on UNIX 4.2 BSD.
Here's what I am hoping to do: I have 50 stations on
a ring, (its a simulation of a token ring network) each time a "token"
visits a station, packet arrival generation is performed(at that station)
by calling on the random number generator ("random"). After a message
is transmitted by the station it passes on the token to the next station.
There, as before, packet arrival generation is performed by calling "random".
What I want, in order to achieve statistical independence, is that each station
have its own sequence of random numbers. Thus I want to initiate 50
sequences of random numbers-each sequence corresponding to a station. Thus
when I want to generate arrivals at station 10, a call is made on the
"random" and it returns the next number in the sequence corresponding
to station 10.
Here's what I did to test if "initstate" and "setstate" were going to give
me what I wanted. I have two programs below. Both the programs test
initstate and setstate for 4 stations.
Program 1: I generate 10 random numbers at station 1(after calling
initstate, and passing it the relevant parameters), then "fork" off
another sequence for station 2 by calling initstate again(this time
giving it a different seed), and once again generate 10 random numbers.
I store the pointer returned by the second call to initstate( this
pointer points to the state of the 1st sequence after it has generated
the 10 random numbers at station 1(at least that's what I think
initstate does-returns a pointer to the previous state), and thus I am
hoping that the pointer returned by the second call to initstate will
give me the state of the 1st stations's sequence, so that if I pass
this pointer to the procedure "setstate" and then call "random" the
number returned by "random" will come from the 1st station's sequence.
Thus having called initstate for each station and generating 10 numbers at
each station, I call "setstate", passing it the pointer that represents
the state of the 1st station's sequence, and the pointer returned by the
1st call to setstate is the state of station 4's sequence after generating
10 numbers. Then I call random again to generate the second set of 10
numbers.( If it works then the second set of 10 numbers must be a
continuation of the sequence that belongs to the station whose
station has been "set".)
Maybe looking at the code will be more clearer than my expalnation.
/************ PROGRAM 1 ******************/
#include <stdio.h>
#include <sys/time.h>
#define BOOLEAN short
#define TRUE 1
#define FALSE 0
main()
{ int i;
char Station1[128], Station2[128], Station3[128], Station4[128];
char *St1, *St2, *St3, *St4, *Jn;
char *initstate();
char *setstate();
Jn = initstate(10, Station1, 128);
printf("For Station 1 ...... \n");
for(i = 1; i <= 10 ; i++)
printf("%d \n", random());
/***********************************/
St1 = initstate(20, Station2, 128);
printf("For Station 2 ...... \n");
for(i = 1;i <= 10 ; i++)
printf("%d \n", random());
/**********************************/
St2 = initstate(30, Station3, 128);
printf("For Station 3 ...... \n");
for(i = 1; i <= 10 ; i++)
printf("%d \n", random());
/**********************************/
St3 = initstate(40, Station4, 128);
printf("For Station 4 ...... \n");
for(i = 1; i <= 10 ; i++)
printf("%d \n", random());
/**********************************/
St4 = setstate(St1);
printf("For Station 1 ...... \n");
for(i = 1; i <= 10 ; i++)
printf("%d \n", random());
/**********************************/
St1 = setstate(St2);
printf("For Station 2 ...... \n");
for(i = 1; i <= 10 ; i++)
printf("%d \n", random());
/**********************************/
St2 = setstate(St3);
printf("For Station 3 ...... \n");
for(i = 1; i <= 10 ; i++)
printf("%d \n", random());
/**********************************/
St3 = setstate(St4);
printf("For Station 4 ...... \n");
for(i = 1; i <= 10 ; i++)
printf("%d \n", random());
/**********************************/
}
/*Program 2: Here I generate 5 numbers(instead of 10 as was done in the first*/
/*program) after calling initstate for each station and storing the pointer.*/
/*The second time around after calling "setstate" the program calls*/
/*random and returns 15 numbers (as opposed to 10 in the 1st program). If*/
/*setstate and initstate work the way I expect, then the set of 20 numbers*/
/*generated at each station should be the same in both program 1 and program 2.*/
/*****************PROGRAM 2 *************************/
#include <stdio.h>
#include <sys/time.h>
#define BOOLEAN short
#define TRUE 1
#define FALSE 0
main()
{ int i;
char Station1[128], Station2[128], Station3[128], Station4[128];
char *St1, *St2, *St3, *St4, *Jn;
char *initstate();
char *setstate();
Jn = initstate(10, Station1, 128);
printf("For Station 1 ...... \n");
for(i = 1; i <= 5 ; i++)
printf("%d \n", random());
/***********************************/
St1 = initstate(20, Station2, 128);
printf("For Station 2 ...... \n");
for(i = 1;i <= 5 ; i++)
printf("%d \n", random());
/**********************************/
St2 = initstate(30, Station3, 128);
printf("For Station 3 ...... \n");
for(i = 1; i <= 5 ; i++)
printf("%d \n", random());
/**********************************/
St3 = initstate(40, Station4, 128);
printf("For Station 4 ...... \n");
for(i = 1; i <= 5 ; i++)
printf("%d \n", random());
/**********************************/
St4 = setstate(St1);
printf("For Station 1 ...... \n");
for(i = 1; i <= 15 ; i++)
printf("%d \n", random());
/**********************************/
St1 = setstate(St2);
printf("For Station 2 ...... \n");
for(i = 1; i <= 15 ; i++)
printf("%d \n", random());
/**********************************/
St2 = setstate(St3);
printf("For Station 3 ...... \n");
for(i = 1; i <= 15 ; i++)
printf("%d \n", random());
/**********************************/
St3 = setstate(St4);
printf("For Station 4 ...... \n");
for(i = 1; i <= 15 ; i++)
printf("%d \n", random());
/**********************************/
}
Any insight into what colud be my mistake (better still how should I do it
to get what I want ) will be deeply appreciated. Please send the solutions
to chauhan at udel-dewey.arpa
I will summarize the responses received and post them on the net.
Thanks in advance
Vijay Chauhan
chauhan at udel-dewey.arpa
More information about the Comp.unix.wizards
mailing list