For this problem you have to decide whether you want a discrete or continuous ra
ID: 3667290 • Letter: F
Question
For this problem you have to decide whether you want a discrete or continuous range of random numbers. After choosing this, you should ask the user the range of numbers they want (assume these values are inclusive). Then you should simply output a random number in the range [min, max]. The sample output below uses default cout settings for displaying numbers (and so should you). You may also assume that the entered minimum is smaller than the maximum. (Hint: You may want to use the constant RAND_MAX for this, which you import from )
Example 1
Do you want a discrete or continuous range (d/c)? c
Enter minimum and maximum of range: 1 3
Random real number from 1 to 3: 2.23926
Example 3
Do you want a discrete or continuous range (d/c)? d
Enter minimum and maximum of range: 100 1000
Random integer number from 100 to 1000: 657
Explanation / Answer
#include<iostream>
#include <ctime>
#include <cstdlib>
#include<random>
int main()
{
char ch;
int l,h;
std::cout<<"Do you want a discrete or continuous range (d/c)?";
std::cin>>ch;
srand (static_cast <unsigned> (time(0)));
switch(ch)
{
case 'd': std::cout<<"Enter minimum and maximum of range:";
std::cin>>l>>h;
std::cout<<"Random integer number from"<<l<<" to"<<h<<":"<<rand()%(h-l)+l;
break;
case 'c': std::cout<<"Enter minimum and maximum of range:";
std::cin>>l>>h;
std::cout<<"Random real number from"<<l<<" to"<<h<<":"<<l + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(h-l)));
break;
default: std::cout<<"wrong choice";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.