【Matlab】怎样随机产生几个数,且这几个数满足一定的条件?具体描述见下:
使用matlab怎样随机产生5个数,且满足一下条件:a+b+c+d+e=691=<a<=301=<b<=301=<c<=301=<d<=241=<e<=24哪位可以帮忙实...
使用matlab怎样随机产生5个数,且满足一下条件:
a+b+c+d+e=69
1=<a<=30
1=<b<=30
1=<c<=30
1=<d<=24
1=<e<=24
哪位可以帮忙实现一下。谢谢 展开
a+b+c+d+e=69
1=<a<=30
1=<b<=30
1=<c<=30
1=<d<=24
1=<e<=24
哪位可以帮忙实现一下。谢谢 展开
2个回答
展开全部
T = 69;
Min1 = 1;
Min2 = 1;
Max1 = 31;
Max2 = 24;
DE = randi([Min2 Max2],2,1);
while 1
LeftOver = T - sum(DE);
ABC = [];
for i = 1:2
ABC = [ABC;randi([Min1 ((LeftOver-1)>Max1)*Max1+(LeftOver-1<=Max1)*(LeftOver-1)],1)];
LeftOver = LeftOver - ABC(end);
end
if LeftOver <= Max1 % 防止最后一个随机数过大
break;
end
end
ABC = [ABC; LeftOver];
Result = [ABC;DE];
Min1 = 1;
Min2 = 1;
Max1 = 31;
Max2 = 24;
DE = randi([Min2 Max2],2,1);
while 1
LeftOver = T - sum(DE);
ABC = [];
for i = 1:2
ABC = [ABC;randi([Min1 ((LeftOver-1)>Max1)*Max1+(LeftOver-1<=Max1)*(LeftOver-1)],1)];
LeftOver = LeftOver - ABC(end);
end
if LeftOver <= Max1 % 防止最后一个随机数过大
break;
end
end
ABC = [ABC; LeftOver];
Result = [ABC;DE];
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
clear;clc
while 1
A=[unifrnd(1,30,1,3),unifrnd(1,24)];
e=69-sum(A);
if(1<=e && e<=24)
A=[A,e]
break;
end
end
while 1
A=[unifrnd(1,30,1,3),unifrnd(1,24)];
e=69-sum(A);
if(1<=e && e<=24)
A=[A,e]
break;
end
end
追问
忘说了,这5个数是个整数,怎么弄。等会分全给你
追答
clear;clc
while 1
A=[randint(1,3,[1,30]),randint(1,1,[1,24])];
e=69-sum(A);
if(1<=e && e<=24)
A=[A,e]
break;
end
end
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询