11 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

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

Does this answer your question?

"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."

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

Sign in to comment.

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

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/451965-generate-an-array-with-random-numbers-from-m-to-n-using-the-command-randi#comment_684898

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/451965-generate-an-array-with-random-numbers-from-m-to-n-using-the-command-randi#comment_684898

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/451965-generate-an-array-with-random-numbers-from-m-to-n-using-the-command-randi#comment_685084

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/451965-generate-an-array-with-random-numbers-from-m-to-n-using-the-command-randi#comment_685084

Sign in to comment.