random number - replaying back.. pls help!

This is a discussion on random number - replaying back.. pls help! within the C++ Programming forums, part of the General Programming Boards category; I'm very new at this and would like a help w/ code below. The problem asks for how many random ...

  1. #1
    Unregistered
    Guest

    Angry 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. #2
    Unregistered
    Guest

    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. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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. #4
    Unregistered
    Guest
    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. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    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. #6
    Unregistered
    Guest
    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. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    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. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    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 subscribe to a feed

Similar Threads

  1. Random Number Issues
    By sansterre in forum C++ Programming
    Replies: 8
    Last Post: 05-15-2009, 06:59 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 02:31 AM
  3. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 03:40 PM
  5. Ask about generate Random number
    By ooosawaddee3 in forum C Programming
    Replies: 2
    Last Post: 07-01-2002, 05:30 AM

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