#### Howdy, Stranger!

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

#### Categories

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!!

• : 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)

• USAMember Posts: 0

________ || http://forcoder.org || free video tutorials and ebooks about [ Assembly Python JavaScript Visual Basic .NET Visual Basic C# Perl Ruby MATLAB Scratch Go Objective-C Delphi Java Swift C++ PHP PL/SQL R C Lua Bash Fortran VBScript F# Ada Crystal SAS Apex Scala ABAP Scheme Rust Awk FoxPro Dart Prolog Lisp LabVIEW Transact-SQL D Kotlin COBOL Hack Alice Julia Clojure ML Logo Erlang ] ___