随机数加到100:Matlab [英] Random numbers that add to 100: Matlab

查看:309
本文介绍了随机数加到100:Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[我正在将总体数分成不同的矩阵,现在想使用随机数测试我的代码.]

[I'm splitting a population number into different matrices and want to test my code using random numbers for now.]

快速提问的人,感谢您的提前帮助-

Quick question guys and thanks for your help in advance -

如果我使用;

 100*rand(9,1)

将这9个数字加到100的最佳方法是什么?

我想要9个0到100之间的随机数,它们总计为100.

I'd like 9 random numbers between 0 and 100 that add up to 100.

是否有一个内置命令可以执行此操作,因为我似乎找不到它.

Is there an inbuilt command that does this because I can't seem to find it.

推荐答案

我经常看到这个错误,有人建议使用给定的总和生成随机数,而只是使用统一的随机集,然后对它们进行缩放.但是,如果这样做的话,结果是真的真正均匀地是随机的吗?

I see the mistake so often, the suggestion that to generate random numbers with a given sum, one just uses a uniform random set, and just scale them. But is the result truly uniformly random if you do it that way?

尝试在两个维度上进行此简单测试.生成一个巨大的随机样本,然后将其缩放为1.我将使用bsxfun进行缩放.

Try this simple test in two dimensions. Generate a huge random sample, then scale them to sum to 1. I'll use bsxfun to do the scaling.

xy = rand(10000000,2);
xy = bsxfun(@times,xy,1./sum(xy,2));
hist(xy(:,1),100)

如果它们确实是真正均匀的随机变量,则x坐标和y坐标将是一致的.任何值均可能发生.实际上,要使两个点的总和等于1,它们必须沿着在(x,y)平面上连接两个点(0,1),(1,0)的线上.为了使这些点保持一致,沿该线的任何点都必须具有相同的可能性.

If they were truly uniformly random, then the x coordinate would be uniform, as would the y coordinate. Any value would be equally likely to happen. In effect, for two points to sum to 1 they must lie along the line that connects the two points (0,1), (1,0) in the (x,y) plane. For the points to be uniform, any point along that line must be equally likely.

当我使用缩放解决方案时,明显的均匀性失败.那条线上的任何一点都不太可能.我们可以看到3维中发生了同样的事情.请参见此处的3-d图,三角形区域中心的点更密集.这是不均匀的反映.

Clearly uniformity fails when I use the scaling solution. Any point on that line is NOT equally likely. We can see the same thing happening in 3-dimensions. See that in the 3-d figure here, the points in the center of the triangular region are more densely packed. This is a reflection of non-uniformity.

xyz = rand(10000,3);
xyz = bsxfun(@times,xyz,1./sum(xyz,2));
plot3(xyz(:,1),xyz(:,2),xyz(:,3),'.')
view(70,35)
box on
grid on

同样,简单的缩放解决方案失败.它根本无法在感兴趣的范围内产生真正统一的结果.

Again, the simple scaling solution fails. It simply does NOT produce truly uniform results over the domain of interest.

我们可以做得更好吗?嗯,是. 2-d中的一个简单解决方案是生成一个随机数,该随机数指定沿连接点(0,1)和1,0)的线的距离.

Can we do better? Well, yes. A simple solution in 2-d is to generate a single random number that designates the distance along the line connecting the points (0,1) and 1,0).

t = rand(10000000,1);
xy = t*[0 1] + (1-t)*[1 0];
hist(xy(:,1),100)

可以证明,现在已经很可能选择了沿方程x + y = 1定义的线的任何点(单位平方).好的直方图反映了这一点.

It can be shown that ANY point along the line defined by the equation x+y = 1, in the unit square, is now equally likely to have been chosen. This is reflected by the nice, flat histogram.

David Schwartz建议的排序技巧是否可以在n维上起作用?显然,它是在2维中进行的,下图表明它是在3维中进行的.无需对此事进行深入思考,我相信它会在n维维度上解决该基本案例.

Does the sort trick suggested by David Schwartz work in n-dimensions? Clearly it does so in 2-d, and the figure below suggests that it does so in 3-dimensions. Without deep thought on the matter, I believe that it will work for this basic case in question, in n-dimensions.

n = 10000;
uv = [zeros(n,1),sort(rand(n,2),2),ones(n,1)];
xyz = diff(uv,[],2);

plot3(xyz(:,1),xyz(:,2),xyz(:,3),'.')
box on
grid on
view(70,35)

还可以下载功能 randfixedsum 从文件交换中,罗杰·斯塔福德(Roger Stafford)的贡献.这是一种更通用的解决方案,可以在任何给定的固定总和下,在单元超立方体中生成真正统一的随机集.因此,要生成位于单元3多维数据集中的随机点集,在约束下它们的总和为1.25 ...

One can also download the function randfixedsum from the file exchange, Roger Stafford's contribution. This is a more general solution to generate truly uniform random sets in the unit hyper-cube, with any given fixed sum. Thus, to generate random sets of points that lie in the unit 3-cube, subject to the constraint they sum to 1.25...

xyz = randfixedsum(3,10000,1.25,0,1)';
plot3(xyz(:,1),xyz(:,2),xyz(:,3),'.')
view(70,35)
box on
grid on

这篇关于随机数加到100:Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆