Question

Write a C++ function that will return a real random number X, where 9.7 ≤ X...

Write a C++ function that will return a real random number X, where 9.7 ≤ X ≤ 39.4

Real numbers only

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code to do so -

#include <iostream>
#include <cstdlib>
using namespace std;
float RandomFloat(float a, float b)
{
    float random = ((float) rand()) / (float) RAND_MAX;
    float diff = b - a;
    float r = random * diff;
    return a + r;
}
int main(int argc, char const *argv[])
{
float ans=RandomFloat(9.7,39.4);
cout<<"returned float value is "<<ans<<endl;
return 0;
}

output -

returned float value is 34.6536

Add a comment
Know the answer?
Add Answer to:
Write a C++ function that will return a real random number X, where 9.7 ≤ X...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT