What is rand() in c programming?

rand() generates pseudo-random number.

It is defined in stdlib.h header file.

The range will be in between 0 to RAND_MAX. RAND_MAX value will be 32767.

Prototype

int rand(void);

Return value

It returns pseudo-random number in the range of 0 to RAND_MAX.


rand in c example program

// C program to illustrate rand function
//Example program for rand() in c
#include<stdio.h>
#include<stdlib.h>
int main()
{
  int i;
  printf("Following are random numbers generated by rand() \n");
  for(i=0;i<3;i++)
   printf("%d \n",rand());
  return 0;
}

Output

Rand in c program

Code explanation

Here the random numbers are generated using rand().

Read GNU Documentation for stdlib.h.


People also ask for

What is rand() in c?

rand() returns pseudo-random number. It is defined in stdlib.h header file.