0

So I want to generate a random number between 0 and 20 and having only a basic knowledge of loops,I figured out that we have to use a rand() function(In python you can easily do it with the rand.random() function but in C there is a difference).

So I tried using :

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int r = rand() % 21;

    printf("The random no is: %d", r);
       
    return 0;
}

but all it did was return the same no every time.I understood that we have to use srand() before this.But I can't understand the concept behind it and how to use it.

Xtense
  • 636
  • 4
  • 13
  • Please have a look at `std::uniform_int_distribution` at https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution – g-217 Nov 22 '20 at 10:53
  • It's actually very convenient to have the same sequence of pseudo-random numbers, when debugging. Otherwise it is very difficult to re-run with the same conditions. To do that, you just "comment out" the `srand(time(NULL))` statement. – Weather Vane Nov 22 '20 at 11:18

0 Answers0