How to generate a random number between 1-200 in C?

+1 vote
asked May 16 by (130 points)
What is the most basic, beginner way to generate a random number between 1-200 in C? Nothing I try is working

2 Answers

+1 vote
answered May 17 by (3,240 points)
Terms are the upper limit, lower limit and the gap between numbers e.g. +1 = 1,2,3,4 while +2 = 2,4,6,8

(rand() % (upper - lower + gap)) + lower;

So to generate a random number between 1-200, with gap between numbers of +1 (1, 2, 3.....):

(rand() % (200 - 1 + 1)) + 1;

Also, in your program, you need to call #include <stdlib.h> header file, since this is where rand function is supported
+2 votes
answered May 17 by (510 points)
#include <stdio.h>
#include <time.h>

void main()
{
  int num;
  srand(time(NULL));
  num=rand()%200+1;
  printf("%d\n",num);
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...