How do I restart a random number sequence.

This is a discussion on How do I restart a random number sequence. within the C Programming forums, part of the General Programming Boards category; I want to restart a random number sequence from anywhere in the sequence ex: random sequence 3 6 7 5 ...

  1. #1
    jeffski
    Guest

    Question 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.

    please help me restart my sequence of random
    numbers.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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;
    	}
    Last edited by Magos; 05-28-2003 at 03:02 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    3
    I know this already.
    problem is not restating the sequence
    its with restarting it at a specified point

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Then do a 'junk-loop', retrieving the first N numbers from the sequence. Then use the rest of the numbers as you like.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    3
    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?

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    3
    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.

    /* 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<stdio.h>/*fopen printf fread fwrite fclose*/
    #include<stdlib.h>/*random*/
    #include<unistd.h>/*getopt*/
    #include<errno.h>/*errno*/
    #include<string.h>/*strerror*/

    enum load_save {neither=0,load,save};

    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;
    enum load_save y=neither;
    initstate(seed,state,STATE_SIZE);

    switch(getopt(argc,argv,"l:s:"))
    {
    case 'l':/*load*/
    fp=fopen(optarg,"r");
    y=load;
    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);
    }
    if(y!=load)
    {
    for(x=0;x<5;x++)
    {/* print the next 5 random sequence of numbers */
    setstate(state);
    printf("%i\n",random()%10);
    }
    }
    else
    {
    fread(saved,1,STATE_SIZE,fp);
    }
    if(y!=save)
    {
    setstate(saved);/* load the saved state*/
    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.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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]
    ... your code here ...
    [/code]

    Something trivial like:
    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;
    }
    Yeah that looks about right. My newline feed might be slightly off, since I just threw this together off the top of my head, but it should suffice.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help regarding random number
    By Bargi in forum C Programming
    Replies: 6
    Last Post: 03-11-2009, 01:16 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 04:16 PM
  4. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 04:23 PM
  5. Ask about generate Random number
    By ooosawaddee3 in forum C Programming
    Replies: 2
    Last Post: 07-01-2002, 04:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21