C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-28-2003, 02:38 PM   #1
jeffski
Guest
 
Posts: n/a
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.
  Reply With Quote
Old 05-28-2003, 03:00 PM   #2
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
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;
	}
__________________
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.

Last edited by Magos; 05-28-2003 at 03:02 PM.
Magos is offline   Reply With Quote
Old 05-28-2003, 03:02 PM   #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
jeffski is offline   Reply With Quote
Old 05-28-2003, 03:03 PM   #4
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
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.
Magos is offline   Reply With Quote
Old 05-28-2003, 03:16 PM   #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?
jeffski is offline   Reply With Quote
Old 05-29-2003, 12:03 PM   #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.

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<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.
jeffski is offline   Reply With Quote
Old 05-29-2003, 02:40 PM   #7
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:59 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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