Write a function that will print a random integer. If no arguments are passed to the function, it will print an integer in the inclusive range from 1 to 100
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:10| Question number:1.10
All Answers
total answers (1)
prtran.m
function prtran(varargin)
% Prints a random integer
% If no arguments are passed, the range is random
% otherwise the input argument(s) specify the range
% Format of call: prtran or prtran(maximum)
% or prtran(minimum, maximum)
% Does not return any values
n = nargin; % number of input arguments
% If no argument passed, range is 1-100
if n == 0
myran = randi([1, 100]);
elseif n == 1
% one argument is max, range is 1-max
myran = randi([1 varargin{1}]);
elseif n == 2
% two arguments are min-max
myran = randi([varargin{1} varargin{2}]);
end
fprintf('The random integer is %d\n', myran)
end
need an explanation for this answer? contact us directly to get an explanation for this answer