mardi 13 octobre 2020

srand and rand creating same number if executed "x" times in one second [duplicate]

My rng code for some reason reproduces only one number if it repeats in one second.

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

int main() {
  for(int i = 1; i <= 100 / 2 ; i++) {
    int now = (int) time(NULL);
    srand(now);
    int rng = rand() % 100 +1;
    printf("%d ", rng);
  }
}

This will produce this (number is random every second):

86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86 86

Do you know what is wrong? Thank you in advance. :)




Aucun commentaire:

Enregistrer un commentaire