# How do I restart a random number sequence.

• 05-28-2003
jeffski
How do I restart a random number sequence.
I want to restart a random number sequence from
anywhere in the sequence

ex:
random sequence
3 6 7 5 3 5 6 2 9 1 2 7 0 9 3 6 0 6 2 6 ...

and I want to restart the sequence say at the first 5

5 3 5 6 2 9 1 2 7 0 9 3 6 0 6 2 6 ...

I read the man page on random and it has the
functions initstate and setstate that I'm unfamiliar
on how to use them. the example given on how to
use them leaves me unsure if they can be used in
this manner. Is there a good way to implement
what I want done with out comming up with
my own (poor) random numer generator.

one method I already thought of is to count the
random numbers that I give out and to use that
many random number over again. But what
happens when the number I am using to count
the random numbers taken wraps.
ex: a one byte number
0 1 2 ... 253 254 255 0 1 2 3 ...

I could reinvent my number so it is impossable for
it to wrap by using a struct with a number and a
pointer. Thus limiting it by memmory with malloc.
but the bigger problem then comes to the time
it takes to get to the part in the sequence.

numbers.
• 05-28-2003
Magos
You initiate a random seed using srand(unsigned int). Every value you pass to it has its own random number sequence. If you want to repeat the numbers again, seed it with the same number over and over. If you don't want the same sequence every time you run the program, generate a 'random' seed which you store for later use:
Code:

```        //Generate a seed         srand(time(0));         int Seed = rand();         //Print the same sequence 10x times         for(int i=0; i<10; i++)         {                 //Use the same seed every time                 srand(Seed);                 //Print 8 numbers from the sequence                 for(int j=0; j<8; j++)                 {                         cout << rand() << " ";                 }                 cout << endl;         }```
• 05-28-2003
jeffski
problem is not restating the sequence
its with restarting it at a specified point
• 05-28-2003
Magos
Then do a 'junk-loop', retrieving the first N numbers from the sequence. Then use the rest of the numbers as you like.
• 05-28-2003
jeffski
Thats the solution I already described.
can't initstate and setstate be used to get
the desired results if not what are they
good for?
• 05-29-2003
jeffski
I just tied that but that doesn't quite work.
That was my first guess on how initstate and setstate would work. I experimented
with it last night and this is what I found to
work.

Quote:

/* Test program by Jeff Sadowski on how to use initstate and setstate
This program helped me figure them out through experimentation. */

#include<time.h>/*time*/
#include<stdlib.h>/*random*/
#include<unistd.h>/*getopt*/
#include<errno.h>/*errno*/
#include<string.h>/*strerror*/

const int STATE_SIZE=256;

int main(int argc,char **argv)
{
unsigned int seed=1;
FILE *fp=NULL;
char state[STATE_SIZE];
char saved[STATE_SIZE];
int x;
initstate(seed,state,STATE_SIZE);

switch(getopt(argc,argv,"l:s:"))
{
fp=fopen(optarg,"r");
break;
case 's':/*save*/
fp=fopen(optarg,"w");
y=save;
break;
default:
if(argc>1)
{
printf("Invalid usage: %s [-l loadfile]||[-s savefile]",argv[0]);
return -1;
}
}
if(y&&fp==NULL)
{
fprintf(stderr,"fopen failed on %s:%s\n",optarg,strerror(errno));
return -1;
}
for(x=0;x<200;x++)
{/* offset it from the seed by an arbitrary number in this case 200
so know I can pick up where I left off */
setstate(state);
random();
}

/* how to save the current state this took me some time to
learn it was not documented well at all. */
memcpy(saved,setstate(state),STATE_SIZE);

if(y==save)
{
fwrite(saved,1,STATE_SIZE,fp);
}
{
for(x=0;x<5;x++)
{/* print the next 5 random sequence of numbers */
setstate(state);
printf("%i\n",random()%10);
}
}
else
{
}
if(y!=save)
{
for(x=0;x<5;x++)
{/* this should start on the same place
in the sequence we saved it at */
setstate(saved);
printf("%i\n",random()%10);
}
}
if(y)
{
fclose(fp);
}
return 0;
}
I'm still working on it but the above seems to work.
• 05-29-2003
quzah
How about you just create the needed function and test it first, rather than having all that other stuff in there? It's usually a good idea to do this when you get stuck.

Also, use code tags whenever you post code.
[code]
```#include <stdio.h> #include <stdlib.h> int main (void ) {     unsigned int i, j;     srand( 5 );     for( i = 0; i < 100; i++ )     {         printf("%3u ", rand() % 555 );         if( i%20 == 19 )             printf("\n");     }     printf("\nEnter a number between 0 and 90: ");     scanf( "%d", &j );     d %= 90;     srand( 5 );     for( i = 0; i < j; i++ )         rand();     printf("The next 10 numbers in sequence are:\n");     for( i = 0; i < 10; i++ );         printf("%3u ", rand() % 555 );     return 0; }```