Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

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

+1 vote
asked May 16, 2020 by Heidi (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, 2020 by LiOS (6,420 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, 2020 by Iftiaq (620 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.
...