# Generate an array with random numbers from M to N using the command randi

12 views (last 30 days)
I am trying to solve the following problem:
Write a function called random_test that takes three input arguments and three output arguments. The function needs to demonstrate that the built-in function randi and rand use the same random number generator. The function generates two arrays of equal size that contains random integers. The arrays must be identical, but the first must be generated using randi and the second using rand. The input arguments to arithmetic must behave like those of randi. That is, the first argument determines the range of the desired random integers. If it is scalar, then it specifies the upper limit (maximum) and in this case, the lower limit (minimum) is 1. If it is a vector, its first element specifies the lower limit, the second element sets the upper limit. The second input argument is the number of rows of the output arrays, while the third is the number of columns. If the third argument is missing, it is assumed to be the same as the second. If the second argument is missing, it is set to 1. The first argument must be provided by the caller. The first and second output argument are the two identical arrays. The third output argument is a logical value: true if the two arrays are really identical and false otherwise. (Note that this needs to be a single logical value and not an array.) The second and third output arguments are optional. Here is an example run:
[R1 R2 c] = random_test([2 5], 3, 8)
R1 =
3 3 2 4 2 4 2 5
4 2 3 3 5 3 2 3
2 2 3 4 2 4 5 4
R2 =
3 3 2 4 2 4 2 5
4 2 3 3 5 3 2 3
2 2 3 4 2 4 5 4
c =
1
I have thought about doing the following and I do not get the expected result:
function [R1, R2, c] = random_test(r, s, t)
R1=randi([r(1),r(2)],s,t);
R21=r(1)+(r(2)-r(1))*rand([s,t]);
R2=round(R21);
c=R1==R2;
end
I think the problem is in generating the elements using the command randi, I do not know if I'm doing that step in the right way, could someone help me please? Thank you

Kodavati Mahendra on 24 Mar 2019
"The function needs to demonstrate that the built-in function randi and rand use the same random number generator"
rand and randi might be using the same random number generator. everytime you call the random number generator, output will be different.
For example,
function [R1, R2, c] = random_test(r, s, t)
R1=randi([r(1),r(2)],s,t);
R21=randi([r(1),r(2)],s,t);
R2=round(R21);
c=R1==R2;
end
will return 0 and not 1. Because both are different instances of rand.
Did I understand your question correctly?
Nathali Londoño Torres on 24 Mar 2019
Hi, I think you do not understand my question correctly. I consider that the problem what it asks is that if the functions rand and randi are called at the same time, they generate the same arrangement of random numbers and therefore c=1. I do not know if the one who misunderstands the question is me then.

Kodavati Mahendra on 24 Mar 2019
function [R1, R2, c] = random_test(r, s, t)
if length(r)==1
r(2) = r;
r(1) = 1;
end
rng('default');
R1=randi([r(1),r(2)],s,t)
R21=r(1)+(r(2)-r(1))*rand([s,t]);
R2=round(R21)
c=(R1==R2);
c
end
"rng('default') resets the random number generator used by RAND, RANDI, and RANDN to its default startup settings, so that RANDI produces the same random numbers as if you restarted MATLAB."

#### 1 Comment

Nathali Londoño Torres on 24 Mar 2019
Thank you very much for your help but I think what I have to show is that always c=1.

Iskender Gencer on 28 Nov 2019
function[R1,R2,c] = random_test(rd,rr,rc)
if nargin <1 || numel(rd) >2
error 'at least one input argument needs to be provided and number of elements can only be 1 or 2'
elseif isscalar(rd) & nargin == 1
rng default
s = rng;
R1 = (1 + (rd-1).*rand(1));
rng(s);
R2 = randi(rd);
R1(R1~=R2 & R1<R2) = ceil(R1(R1~=R2 & R1<R2));
R1(R1~=R2 & R1>R2) = floor(R1(R1~=R2 & R1>R2));
c = isequal(R1,R2);
elseif isvector(rd) & nargin ==1 & numel(rd) == 2
rng default
s = rng;
R1 = (rd(1) + (rd(2)-1).*rand(1));
rng(s);
R2 = randi(rd);
R1(R1~=R2 & R1<R2) = ceil(R1(R1~=R2 & R1<R2));
R1(R1~=R2 & R1>R2) = floor(R1(R1~=R2 & R1>R2));
c = isequal(R1,R2);
elseif isscalar(rd) & nargin == 2
rng default
s = rng;
R1 = (1 + (rd-1).*rand(rr,rr));
rng(s);
R2 = randi(rd,rr,rr);
R1(R1~=R2 & R1<R2) = ceil(R1(R1~=R2 & R1<R2));
R1(R1~=R2 & R1>R2) = floor(R1(R1~=R2 & R1>R2));
c = isequal(R1,R2);
elseif isscalar(rd) & nargin ==3
rng default
s = rng;
R1 = (1 + (rd-1).*rand(rr,rc));
rng(s);
R2 = randi(rd,rr,rc);
R1(R1~=R2 & R1<R2) = ceil(R1(R1~=R2 & R1<R2));
R1(R1~=R2 & R1>R2) = floor(R1(R1~=R2 & R1>R2));
c = isequal(R1,R2);
else
rng default
s = rng;
R1 = (rd(1) + (rd(2)-1).*(rand(rr,rc)));
rng(s);
R2 = randi(rd,rr,rc);
R1(R1~=R2 & R1<R2) = ceil(R1(R1~=R2 & R1<R2));
R1(R1~=R2 & R1>R2) = floor(R1(R1~=R2 & R1>R2));
c = isequal(R1,R2);
end

Stephen Cobeldick on 28 Nov 2019
>> R = [2,5];
>> rng('default');
>> A = randi(R,3,8)
A =
5 5 3 5 5 2 5 2
5 4 4 2 3 3 5 5
2 2 5 5 5 5 4 5
>> rng('default');
>> B = R(1)+fix((1+diff(R))*rand(3,8))
B =
5 5 3 5 5 2 5 2
5 4 4 2 3 3 5 5
2 2 5 5 5 5 4 5
>> C = isequal(A,B)
C =
1