Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a) (2 pts) Write a Matlab function f = randnum(a,b) which takes as input two int

ID: 1860542 • Letter: A

Question

a) (2 pts) Write a Matlab function f = randnum(a,b) which takes as input two integers a and b, and generates and returns a random integer that is equally likely to take any value between a and b (inclusive).  Save this function in the file randnum.m.

(b) (2 pts) Write a Matlab m-file test_randnum.m that tests the accuracy of your randnum() function.   This program should use randnum() to generate 100,000 random numbers between 1 and 10.  Store each random number in a vector x (it will be of length 100,000 when the simulation is done).  Then display a histogram of the results using the hist(x,1:10) function.

(c) (4 pts) Write a Matlab function f = randnum_reject(a,b,c) which takes as input three integers a, b and c, and generates and returns a random integer that is equally likely to take any value between a and b (inclusive), but will not return the value given by c.  Save this function in the file randnum_reject.m.

(d) (2 pts)Write a Matlab m-file test_randnum_reject.m that tests the accuracy of your randnum_reject() function. This program should userandnum_reject() to generate 100,000 random numbers between 11 and 20 excluding 15.  Store each random number in a vector x (it will be of length 100,000 when the simulation is done).  Then display a histogram of the results using the hist(x,11:20) function.

Explanation / Answer

1.


function c = randnum(a,b)


c = randi([a b],100000,1);


2.

x= randnum(1,10)


hist(x,1:10)


3.

function f = randnum_reject(a,b,c)


f = randi([a b],100000,1);