C++ Programming Code Examples C++ > Beginners Lab Assignments Code Examples C++ Programming Code to Generate Random Numbers C++ Programming Code to Generate Random Numbers To generate random numbers in C++ programming, use the function rand() to generate and print random numbers. And if you want to generate different-different random numbers at each time when you compile and run the same program, then use the function srand() before generating the random numbers using the function rand() as shown here in the following program. Following C++ program generate 100 random numbers using rand() function of stdlib.h library: #include<iostream.h> #include<conio.h> #include<stdlib.h> void main() { clrscr(); int i; for(i=0; i<100; i++) { cout<<rand()<<"\t"; } getch(); }