logo

Random numbers in an interval in C

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> int rand_interval(int a, int b) { return rand() % (b - a) + a; } int main(int argc, char* argv[]) { srand(time(NULL)); for (int i = 0; i < 100; i++) { printf("%d ", rand_interval(5, 20)); } return 0; }

Courses with this lesson