Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

RANDOM FUNCTIONS....PLEASE HELP!!!!!!!!!!!

I HAVE TO WRITE A PROGRAM GENERATING 300 RANDOM NUMBERS BETWEEN 0 AND 1...WHICH WOULD MAKE THEM ALL DECIMALS....AND THEN ADD THEM ALL UP AND DIVIDE THE TOTAL BY 300 TO GET THE SAMPLE MEAN....THEN I HAVE TO CALCULATE THE SAMPLE VARIANCE.......THE PROGRAM I WROTE USING THE RANDOM NUMBER GENERATOR GENERATED 300 NUMBERS....EITHER 0 OR 1........I CANT GET IT TO GENERATE A FLOAT INSTEAD OF AN INTEGER......CAN U PLEASE HELP ME...ANY HELP IS GREATLY APPRECIATED!!


Comments

  • : I HAVE TO WRITE A PROGRAM GENERATING 300 RANDOM NUMBERS BETWEEN 0 AND 1...WHICH WOULD MAKE THEM ALL DECIMALS....AND THEN ADD THEM ALL UP AND DIVIDE THE TOTAL BY 300 TO GET THE SAMPLE MEAN....THEN I HAVE TO CALCULATE THE SAMPLE VARIANCE.......THE PROGRAM I WROTE USING THE RANDOM NUMBER GENERATOR GENERATED 300 NUMBERS....EITHER 0 OR 1........I CANT GET IT TO GENERATE A FLOAT INSTEAD OF AN INTEGER......CAN U PLEASE HELP ME...ANY HELP IS GREATLY APPRECIATED!!


    well kay, one way to generate fractional numbers from an integer random number generator is to cast the integer output to a float and divide it by the maximum output.


    eg ( rand() uses the library stdlib.h)


    int n;

    float f;


    n=rand();


    f=(float)(n);

    f/=(float)(RAND_MAX); /* maximum rand() output. */


    hth

    thomas kay (no relation)


Sign In or Register to comment.