mercredi 1 novembre 2023

Is "srand(time(0) * int_value))" a good way to get different values for each execution?

I want to get random float values between 0 and 10 for each execution, and if i don't use **srand(time(0)), **my random values always same in each execution. However, if i use this, i get different values but it changes like 0.01 between two consecutive executions.

So my question is this:

If I use srand(time(0) * 1000), I get different random numbers for each execution. Is this a proper way to use like this, or will there be an overflow because of seed limits or something like that for a specific times?

Here is a simple function to get different float values between 0 and 10

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

int main() {

    srand(time(0) * 1000);

    int i;
    for (i = 0; i < 10; i++) {
        float randomValue = ((float)rand() / RAND_MAX) * 10;
        printf("Random float value %d: %.2f\n", i + 1, randomValue);
    }

    return 0;
}



Aucun commentaire:

Enregistrer un commentaire