mercredi 19 octobre 2016

Trying to accumulate and sort random numbers into bins

I've create a list of random values in C, using the code below: I need to somehow sort these values into 200 bins distributed over the interval from 0 to 4, then write information about the bins to a file, though right now I'm only concerned with the former. How can I write a loop that dynamically creates bins on the interval and sorts the output from the random number generator into them?

int main(){
//declare vars, functions, and constants
#define NRAND 20000
int i, j;
double X, fX, Y, bin[200][100];
double Xdrand = (double) random()/(double) RAND_MAX;
FILE *fp;//declarations for file, opens file
fp = fopen("exp_rand.dat", "w+");
for (i=0; i<=NRAND; i++)//creates NRAND random values
{

    if(Xdrand!=1)//ensures value is not 1, scales random value to interval
    {
        Xdrand=(double) random()/(double) RAND_MAX;
        X=Xdrand*4;
        Y=Xdrand*2;
        fX=1/(exp(X));
        if(Y<=fX)
        {
            X=Y;
            return;
        }
    }       
}




Aucun commentaire:

Enregistrer un commentaire