Answer to Question #458 in C++ for lily
2010-07-14T22:05:48-04:00
Write the statement(or statements) that uses function rand() to determine a random floating point number between 12.5 and 24.5 and assigns the number to variable ran1. How can I do this?
1
2010-07-15T05:15:20-0400
You can do this next way: 1 #include "stdio.h" 2 #include <stdlib.h> 3 #include <time.h> 4 5 float randfloat() { 6 ....int i1 = rand(), 7 ........i2; 8 ....do { 9 ........i2 = rand(); 10 ....} while (i2 < i1); 11 ....return i1*1./i2; 12 } 13 float randfloat(float from, float to) { 14 ....return from + (to-from)*randfloat(); 15 } 16 17 int main() { 18 ....srand(time(NULL)); 19 ....printf("%f %f %f\n", randfloat(12.5, 24.5), randfloat(12.5, 24.5), randfloa t(12.5, 24.5)); 20 ....return 0; 21 }
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS !
Learn more about our help with Assignments:
C++
Comments
Leave a comment