I want to generate five random numbers each time using srand() function within the range 1 to 10. Here is a sample program:
#include<stdio.h>
#include<math.h>
#define size 10
int main()
{
int A[5];
for(int i=0;i<5;i++)
{
A[i]=srand()%size
}
}
But I am getting an error saying too few arguments for the function srand(). What will be the solution?
srand
sets the seed for rand
, the pseudo-random number generator.
Corrected code:
#include <stdio.h>
#include <math.h> /* Unused header */
#include <stdlib.h> /* For `rand` and `srand` */
#include <time.h> /* For `time` */
#define size 10
int main()
{
int A[5];
srand(time(NULL)); /* Seed `rand` with the current time */
for(int i = 0; i < 5; i++)
{
A[i] = rand() % size; // `rand() % size` generates a number between 0 (inclusive) and 10 (exclusive)
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments