Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Beginner
Dec 8, 1998



Building the Right Environment to Support AI, Machine Learning and Deep Learning

How to Generate a Sequence of Random Numbers

Standard C provides two functions for the purpose of random number generation--rand and srand. The function rand generates a sequence of pseudo-random numbers. The term pseudo-random implies that the generated sequence is not really random: no matter how many times you execute the program, the same sequence of numbers will be generated. To create a truly unique sequence of numbers every time the program executes, you should use the function srand:
#include <cstdlib> //rand and srand declarations
#include <cstdio> //printf declaration
#include <ctime> //time declaration
using namespace std;

void main() 
 srand ( time(NULL) ); //set starting-point for a random sequence of numbers 

 for (int j =0; j < 10; j++)
   printf ("%d\n", rand() );
Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date