how to make a function that generate uniformely distributed random matrix

Illustration
Muhammad Usman Saleem - 2022-06-11T13:16:41+00:00
Question: how to make a function that generate uniformely distributed random matrix

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 answered . 2025-11-20

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)

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!