![]() |
| | #1 |
| Guest
Posts: n/a
| 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 |
| Confused 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 | |
| | #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 | |
| | #4 |
| Confused 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 | |
| | #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 | |
| | #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:
| |
| jeffski is offline | |
| | #7 |
| +++ OK NO CARRIER 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;
}
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |