Home › C Tutorial › C random numbers
C random numbers
rand and srand.
Seed once with srand(time(NULL)), then use rand() % n for 0 to n-1.
Example code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
printf("%d\n", rand() % 6 + 1);
return 0;
}