I'm learning C and I want to generate a number between 0 and 6400. This is the code I came up with:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(0));
int i = (rand() % 6401);
printf("Random number between 0 and 6400: %d\n", i);
return 0;
}
When I compile and run this code from the command line I get some very weird results:
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6282
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6282
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6285
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6285
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6289
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6289
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6292
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6292
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6295
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6298
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6298
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6302
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6302
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6305
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6305
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6308
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6308
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6311
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6311
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6315
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6315
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6318
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6318
K:\C\Labo\Oefeningen 2019>a
Random number between 0 and 6400: 6321
K:\C\Labo\Oefeningen 2019>
The numbers are all different but I would expect a somewhat even distribution between 0 and 6400. The weird thing is that I was using the same function without problems an hour ago? (I was using it to generate smaller numbers before.) I'm certain it's something really stupid that I'm missing but I've been stuck for an hour now.
Aucun commentaire:
Enregistrer un commentaire