Need help on a homework problem

4 views (last 30 days)
Chante
Chante on 11 Nov 2022
Commented: Star Strider on 11 Nov 2022
I am having trouble trying to denote that the min and max values are bounds in my code. Here is what I have so far:
function int = MyRand(varargin)
n=nargin;
if n == 0
int=randi([1,100],1);
elseif n == 1
x=max(MyRand());
int=randi([0,x],1);
elseif n == 2
y=min(MyRand());
x=max(MyRand());
int=randi([x,y],1);
end
end
With no input variables, the function is working. When I get to one, it doesn't exactly function the way it should– the input value isn't the max for the random number. When I input two variables, an error shows up. I would appreciate any assistance in helping solve this code, and I have attached my homework problem here:
Write a function MyRand() that will output one random number (must be a positive integer). This function can receive variable number of input arguments. If no arguments are passed to the function, it will return one random number from 1 to 100. If one argument is passed to the function, it is the upper limit and the random number produced will be from 0 to this upper limit. If two arguments are passed, they represent the lower limit and the upper limit; and the function will return one random number within the range of [LowerLimit, UpperLimit].
For example, when testing the function in the command window, it can work as below: >> MyRand()
ans =
18
>> MyRand(10)
ans =
7
>> MyRand(90, 95)
ans =
93

Answers (1)

Star Strider
Star Strider on 11 Nov 2022
Try something like this —
result = MyRand
result = 60
result = MyRand(10)
result = 74
result = MyRand(90, 95)
Error using randi
First input must be a positive scalar integer value IMAX, or two integer values [IMIN IMAX] with IMIN less than or equal to IMAX.

Error in solution>MyRand (line 15)
int=randi([x,y],1);
function int = MyRand(a,b)
n=nargin;
if n == 0
int=randi([1,100],1);
elseif n == 1
x=max(MyRand());
int=randi([0,x],1);
elseif n == 2
y=min(MyRand());
x=max(MyRand());
int=randi([x,y],1);
end
end
The function likely has to have defined arguments, regardless of what they actually are.
It may be necessary to put in a test to be certain that x<y in the last instance.
Also, recursion (a function calling itself) can lead to problems.
.
  2 Comments
Steven Lord
Steven Lord on 11 Nov 2022
Nowhere in this code are the input arguments a or b used.
function int = MyRand(a,b)
n=nargin;
if n == 0
upper = ?
lower = ?
elseif n == 1
upper = ?
lower = ?
else % n has to be 2 here so no need for if. Do you understand why?
upper = ?
lower = ?
end
% Now use lower and upper
Fill in the question marks with appropriate values for the lower and upper bounds (these sections of code may use a and/or b) and then fill in one more line of code that defines int using the variables lower and upper.
There is some simplification you can do to this code, but this should give you a framework on which to implement your homework.
Star Strider
Star Strider on 11 Nov 2022
The original code actually doesn’t appear to use the arguments, the reason I called them ‘a’ and ‘b’ here. It just uses the number of arguments to use the appropriate code in the if block.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!