dimanche 21 avril 2019

Why my function returns so big random values when it has a range?

I'm trying to get random float values in a range in a pointer but it gives me some very big numbers. I think my error is in the function calling but i'm not sure.

This is the function:

#include <time.h>
#include <stdlib.h>
void random_values(float *arr, int N) // "arr" is used like an array to 
                                      //keep random float values

{                                     // "N" is the length of the array
    int i;
    float max_range=1000.00;
    arr=(float *)(malloc(sizeof(float)*N));
    srand(time(0));
    for(i=0; i<N; i++)
    {
        *(arr+i)=((float)rand()/(float)(RAND_MAX)) * max_range;

    }
    free(arr);
}

Here I call the function and try to print it out:

random_values(arr, length);
for(i=0;i<length;i++)
    printf("%.2f\n", *(arr+i));




Aucun commentaire:

Enregistrer un commentaire