devxlogo

How to Generate a Sequence of Random Numbers

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  //rand and srand declarations#include  //printf declaration#include  //time declarationusing 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
", rand() );}
See also  The CTO's Guide on Best Practices for Developing Top-Notch SaaS Applications
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist