Generate an array with random numbers from M to N using the command randi
조회 수: 11 (최근 30일)
이전 댓글 표시
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
댓글 수: 2
Kodavati Mahendra
2019년 3월 24일
편집: Kodavati Mahendra
2019년 3월 24일
"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?
답변 (4개)
Kodavati Mahendra
2019년 3월 24일
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
2019년 11월 28일
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
댓글 수: 0
Stephen23
2019년 11월 28일
>> 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
댓글 수: 0
JIAQING QIAN
2020년 5월 9일
I think the following code will work for this question. I've tried different input data and the fuction can always return c=1.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!