使用randi命令生成一个从M到N的随机数数组

41次浏览(最近30天)
Nathali Londono托雷斯
Nathali Londono托雷斯 2019年3月24日
回答: 嘉庆钱2020年5月9日
我正在尝试解决以下问题:
编写一个名为random_test的函数,接受三个输入参数和三个输出参数。该函数需要演示内置函数randi和rand使用相同的随机数生成器。该函数生成两个大小相等的数组,其中包含随机整数。这两个数组必须相同,但第一个数组必须使用randi生成,第二个数组必须使用rand生成。算术的输入参数必须像randi的输入参数一样。也就是说,第一个参数确定所需的随机整数的范围。如果它是标量,那么它指定上限(最大值),在本例中,下限(最小值)是1。如果它是一个向量,它的第一个元素指定下限,第二个元素设置上限。第二个输入参数是输出数组的行数,而第三个参数是列数。如果缺少第三个参数,则假定它与第二个参数相同。 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
我想过做以下事情,但没有得到预期的结果:
函数[R1, R2, c] = random_test(r, s, t)
R1 =兰迪([r(1),(2)),年代,t);
一下R21 = r (1) + (r - r(1)(2)) *兰德((s, t));
R2 =圆(一下R21);
c = = = R2;
结束
我认为问题在于使用命令生成元素 兰迪 我不知道这一步做得对不对,有人能帮我一下吗?谢谢你!
2的评论
Nathali Londono托雷斯
Nathali Londono托雷斯 2019年3月24日
嗨,我认为你没有正确理解我的问题。我认为问题是如果函数 兰德 兰迪 同时调用,它们产生相同的随机数排列,因此c=1。我不知道那个误解这个问题的人是不是我。

登录评论。

答案(4)

Kodavati Mahendra
Kodavati Mahendra 2019年3月24日
函数[R1, R2, c] = random_test(r, s, t)
如果长度(r) = = 1
(2) = r;
r (1) = 1;
结束
rng (“默认”);
R1 =兰迪([r(1),(2)),年代,t)
一下R21 = r (1) + (r - r(1)(2)) *兰德((s, t));
R2 =圆(一下R21)
c = (R1 = = R2);
c
结束
这回答了你的问题吗?
rng('default')重置随机数生成器使用的RAND, RANDI,和RANDN到它的默认启动设置,使RANDI产生相同的随机数,如果你重新启动MATLAB。

Iskender Gencer
Iskender Gencer 2019年11月28日
函数(R1, R2, c) = random_test (rd, rr, rc)
如果Nargin <1 || numel(rd) >2
错误'至少需要提供一个输入参数,且元素数量只能为1或2'
elseifIsscalar (rd) & nargin == 1
rng默认的
s =提高;
R1 = (1 + (rd-1).*rand(1));
rng(年代);
R2 =兰迪(rd);
R1(R1~=R2 & R1
R1(R1~=R2 & R1 R2) = floor(R1(R1~=R2 & R1>R2));
c = isequal (R1, R2);
elseifIsvector (rd) & nargin ==1 & numel(rd) == 2
rng默认的
s =提高;
R1 = (rd(1) + (rd(2)-1).*rand(1));
rng(年代);
R2 =兰迪(rd);
R1(R1~=R2 & R1
R1(R1~=R2 & R1 R2) = floor(R1(R1~=R2 & R1>R2));
c = isequal (R1, R2);
elseifIsscalar (rd) & nargin == 2
rng默认的
s =提高;
R1 = (1 + (rd-1).*rand(rr,rr));
rng(年代);
R2 =兰迪(rr, rd rr);
R1(R1~=R2 & R1
R1(R1~=R2 & R1 R2) = floor(R1(R1~=R2 & R1>R2));
c = isequal (R1, R2);
elseifIsscalar (rd) & nargin ==3
rng默认的
s =提高;
R1 = (1 + (rd-1).*rand(rr,rc));
rng(年代);
R2 =兰迪(rr, rd rc);
R1(R1~=R2 & R1
R1(R1~=R2 & R1 R2) = floor(R1(R1~=R2 & R1>R2));
c = isequal (R1, R2);
其他的
rng默认的
s =提高;
R1 = (rd(1) + (rd(2)-1).*(rand(rr,rc)));
rng(年代);
R2 =兰迪(rr, rd rc);
R1(R1~=R2 & R1
R1(R1~=R2 & R1 R2) = floor(R1(R1~=R2 & R1>R2));
c = isequal (R1, R2);
结束

斯蒂芬。
斯蒂芬。 2019年11月28日
>> r = [2,5];
> > rng (“默认”);
>> A = randi(R,3,8)
一个=
5 5 3 5 5 2 5
5 4 4 2 3 3 5 5
2 2 5 5 5 5 4 5
> > rng (“默认”);
>> B = R(1)+fix((1+diff(R))*rand(3,8))
B =
5 5 3 5 5 2 5
5 4 4 2 3 3 5 5
2 2 5 5 5 5 4 5
>> C = isequal(A,B)
C =
1

嘉庆钱
嘉庆钱 2020年5月9日
我认为下面的代码可以解决这个问题。我尝试了不同的输入数据,函数总是可以返回c=1。

社区寻宝

在MATLAB中心找到宝藏,并发现社区如何可以帮助你!

开始狩猎!