how to make a function that generate uniformely distributed random matrix

Muhammad Usman Saleem · Jun 11, 2022 · 1.9K views
Question
Hi every one..   I am going to make a function which takes three input arguments limit,a,b in that order. The function returns an a-by-b matrix of uniformly distributed random integers between 1 and limit inclusive. I am not allowed to use randi, but i can use rand. To make sure that my result is indeed uniformly distributed, test the output of the function by using the built-in function hist, which plots a histogram.   to make such function i am using that codes     function rad=rad1(limit,n,m) mat=zeros(n,m); for i=1:n for j=1:m a=rand(1,limit); mat(i,j)=floor(a) end end end but i getting error every time.Can anybody assist me to make such function.
Expert Answer
Profile picture of Neeta Dsouza
Neeta Dsouza PhD Expert
Answered Aug 23, 2026

Define your function as

 

 function r = myrandi(limit, a, b)
 r = ceil(limit*rand(a,b));

Check with

 a= 10; b = 3000; limit = 12;
 r = myrandi(limit, a, b); 
 hist(r(:), 1:limit)

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!