Q:

C++ program to generate random numbers

0

C++ program to generate random numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Generate random numbers example in C++

/*C++ program to generate random numbers.*/
 
#include<iostream.h>
#include<stdlib.h>
 
int main()
{
    int i;          //loop counter
    int num;        //store random number
 
    randomize();    //call it once to generate random number
    for(i=1;i<=10;i++)
    {
        num=rand()%100; //get random number
        cout << num << "\t";
    }
    return 0;
}

Output

    First run:
    88 	90	76	3	67	43	20	31	28	1
    Second run:
    19	89	87	6	99	25	66	29	96	69

If you do not use randomize(), numbers will be generated randomly but when you again run the program same output will come.
the output after removing randomize () :

    First run:
    66	8	90	99	7	45	15	51	44	80
    Second run:
    66	8	90	99	7	45	15	51	44	80

Note: always use randomize (), if you want to get random numbers at every run.

In this program, we used # just after about yourself information, because in the program we are using delimiter (#) to terminate string because about yourself section may contain more than one lines, so we have to need a special delimiter to terminate reading.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
Print Reverse Triangle Bridge Pattern for Characte... >>
<< C++ program to read string using cin.getline()...