mardi 27 avril 2021

Generate Random numbers using C

I want to initialize certain weights for training a neural network using C. However, I'm facing issues in initializing weights. Here's what I've tried -

double generate_random_num(){
    srand(time(NULL));
    double num = ((double)rand()/(double)RAND_MAX)*2.0 - 0.1;
    return num;
}

int main(){
    double x = generate_random_num();
    printf("%lf\n", x);
}

I get the outputs similar when I tried to run the program various times -

1.541774
1.510279
1.504480
1.502100

The generated numbers are close to 1.5 every time. It's not uniform over a range of values. How to generate random values which are uniform over a range?




Aucun commentaire:

Enregistrer un commentaire