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 Nov 20, 2025

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)

 

Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!