Thread: Random Output Numbers

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    Random Output Numbers

    Here is my code:

    Code:
    #include <cstdlib> 
    #include <ctime> 
    #include <iostream>
    #include <stdio.h>
    
    using namespace std;
    void myflush(std::istream& in)
    {
        in.clear();
        in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    
    void mypause()
    {
        
        myflush(std::cin);
        std::cin.get();
    }
    int main(void)   
    { 
        srand((unsigned)time(0)); 
        int random_integer; 
        for(int index=0; index<20; index++){ 
            random_integer = (rand()%10)+1; 
            cout << random_integer << endl; 
            mypause();
        } 
    }
    I want it so this program Outputs 5 Random numbers instead of one number.

    so I want 5 Random numbers such as "19, 9, 1, 6, 8" instead of only one Random number.

    So. How would I do this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm guessing you mean five at a time before the pause, since you are outputting twenty random numbers? Why doesn't this work?
    Code:
        for(int index=0; index<5; index++){
            random_integer = (rand()%10)+1;
            if (index) cout << ", ";
            cout << random_integer;
        }
        cout << endl;
        mypause();
    }

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Ok. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generatin Random Numbers
    By dantu1985 in forum C Programming
    Replies: 15
    Last Post: 08-13-2007, 01:21 AM
  2. Random numbers
    By Mavix in forum C Programming
    Replies: 3
    Last Post: 05-13-2007, 09:01 AM
  3. preventing same random numbers in JAVA
    By Rumproast23 in forum Tech Board
    Replies: 10
    Last Post: 04-04-2007, 06:46 AM
  4. Random Numbers
    By glider_pilot123 in forum C Programming
    Replies: 4
    Last Post: 11-03-2004, 02:12 PM
  5. Generating Random Numbers
    By FromHolland in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2003, 09:05 AM