# Thread: random number - replaying back.. pls help!

1. ## random number - replaying back.. pls help!

I'm very new at this and would like a help w/ code below.
The problem asks for how many random number the user wants but when I run it... it always gives 10 eventhough I just ask for 3!
Pls help. Thanks!

int status = 0, n, i;
unsigned initial_seed;
Random my_rand;

cout << "This program will generate random numbers that you can retrieve back" << endl;
cout << "How many random numbers do you want generated?" << endl;
cin >> n;

for (i=n; i<n; i++)
cout << n <<": " << my_rand.get_rand() << endl;
cout << "Initial seed is " << my_rand.get_seed() << endl;

//cout << "seed is " << my_rand.get_seed() << endl;
//reset sequence
my_rand.set_seed(initial_seed);

//for(i=0; i<10; i++)
// cout << i <<": " << my_rand.get_rand() << endl;

Also, how do I make it so I can add a "sequence # to that set of rand. #'s so when the user asks for that particular set, it will show it back? 2. ## Re: random number - replaying back.. pls help!

sorry changed the cout to "i" below (marked ->).

Originally posted by Unregistered
I'm very new at this and would like a help w/ code below.
The problem asks for how many random number the user wants but when I run it... it always gives 10 eventhough I just ask for 3!
Pls help. Thanks!

int status = 0, n, i;
unsigned initial_seed;
Random my_rand;

cout << "This program will generate random numbers that you can retrieve back" << endl;
cout << "How many random numbers do you want generated?" << endl;
cin >> n;

for (i=n; i<n; i++)
-> cout << i <<": " << my_rand.get_rand() << endl;
cout << "Initial seed is " << my_rand.get_seed() << endl;

//cout << "seed is " << my_rand.get_seed() << endl;
//reset sequence
my_rand.set_seed(initial_seed);

//for(i=0; i<10; i++)
// cout << i <<": " << my_rand.get_rand() << endl;

Also, how do I make it so I can add a "sequence # to that set of rand. #'s so when the user asks for that particular set, it will show it back? 3. Code:
```for (i=n; i<n; i++)
cout << i <<": " << my_rand.get_rand() << endl;
//It appears this for-loop will get executed 0 times, as i=n at the start, so it will never be less than n.  Perhaps you want:

for (i=0; i<n; i++)
cout << i <<": " << my_rand.get_rand() << endl;
//This will execute n times``` 4. thanks swoopy!

though i have another one: once the user is given that set of random #'s requested, how do i make a "sequence" to that set of random #'s so when the user wants to "retrieve" that sequence, he/she gets the same set of random #'s.??

thanks for any help! 5. Have the user input the seed and then seed the rand function with that seed

cin>>seed;
srand(seed);

now, whenever the user inputs the same seed and the same number of numbers they want to see, they will see the same exact sequence. 6. i'm really not understanding this yet.. would you mind looking at my code & tell me where to ask the user to input the seed & where to srand it??
thank you much!

void choice_one(void)
{
int i, n;
CRandom my_rand;

cout << "How many random numbers do you want generated?" << endl;
cin >> n;

for (i=0; i<n; i++)
cout << i <<": " << my_rand.get_rand() << endl;
cout << "Initial seed is " << my_rand.get_seed() << endl;

} 7. you know the seed that u get returned in your function, create another function that allows you to set the seed.

kinda like

Code:
```int seed;
cout<<"Input seed";
cin>>seed;
srand(seed);
cout<<"Seed has been set to "<<seed;``` 8. Or you can store the values in an array

Code:
```int rand_array[BUF_SIZE];

for (i=n; i<n; i++)
cout << i <<": " << my_rand.get_rand() << endl;
//It appears this for-loop will get executed 0 times, as i=n at the start, so it will never be less than n.  Perhaps you want:

for (i=0; i<n; i++)
cout << i <<": " << (rand_array[n] = my_rand.get_rand()) << endl;
//This will execute n times

//replay the rand sequence
for (i = 0; i < n; i++)
cout << i << ": " << rand_array[i] << endl;``` Popular pages Recent additions 