In: Computer Science
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for a minimum and maximum real number, then generates and prints a random number in the requested range. The script should then do the same for an integer range. Sample output: Enter a minimum real value: 0.5 Enter a maximum real value: 30 A random number in the range ( 0.5000, 30.0000 ) is 1.7851 Enter a minimum integer value: -10 Enter a maximum integer value: 20 A random integer in the range [ -10, 20 ] is 8 Enter a minimum real value: -5 Enter a maximum real value: 5 A random number in the range ( -5.0000, 5.0000 ) is -4.5644 Enter a minimum integer value: 50 Enter a maximum integer value: 75 A random integer in the range [ 50, 75 ] is 65
Please find the required MATLAB scripts:
For real numbers:
%========================================================================
clear all;
clc;
min_real_val = input('Enter a minimum real value: ');
max_real_val = input('Enter a maximum real value: ');
num_real_rand =
min_real_val+(max_real_val-min_real_val)*rand;
fprintf('A random number in the range ( %f, %f ) is
%f\n',min_real_val,max_real_val,num_real_rand);
%========================================================================
For integer numbers:
%========================================================================
min_int_val = input('Enter a minimum integer value: ');
max_int_val = input('Enter a maximum integer value: ');
num_int_rand = randi([min_int_val,max_int_val]);
fprintf('A random integer in the range [ %d, %d ] is
%d\n',min_int_val,max_int_val,num_int_rand);
%========================================================================
Sample output:
Enter a minimum real value: 0.33
Enter a maximum real value: 1.7
A random number in the range ( 0.330000, 1.700000 ) is
0.524384
Enter a minimum integer value: -10
Enter a maximum integer value: 40
A random integer in the range [ -10, 40 ] is 11
Hope this helps!
************* PLEASE THUMBS UP!!!!!!!! *************
In case of any clarification, please comment!