我期待生成一个数字序列,每个数字都是70到100之间会有X上的序列中的数字,它会给平均Y的。什么这个算法是什么样子?
I am looking to generate a number sequence where each number is between 70 and 100 there will be x numbers in the sequence and it will give and average of y. What would this algorithm look like?
我觉得这是不可能的,他们70和100,有一个给定的平均同时之间均匀分布的。
I think it is impossible for them to be uniformly distributed between 70 and 100 and have a given average at the same time.
你可以做的就是生成随机数有一个给定的平均值,然后扩展他们融入[70,100](但他们不会均匀分布有)。
What you can do is generate random numbers that have a given average and then scale them to fit into [70, 100] (but they will not be uniformly distributed there).
生成随机数[0..1(
generate random numbers [0..1(
计算其平均
乘他们都符合要求的平均
multiply all of them to match the required average
如果其中任何一个不适合到[70,100],规模个个再次通过相同的因子减少从是
的距离(这不改变的平均值)。 X [I] = Y +(X [I] - Y)*比例
if any of them does not fit into [70, 100], scale all of them again by reducing their distance from y
by the same factor (this does not change the average). x[i] = y + (x[i] - y)*scale
您将结束与该均在范围[70,100(数字,但它们将跨越该中心位于Y A不同(但重叠)间隔均匀分布。而且,这种方法只适用于真正的/浮点数字。如果你想整数,你在你的手中有一个组合的问题。
You will end up with numbers that are all in the range [70, 100(, but they will be uniformly distributed across a different (but overlapping) interval that is centered on y. Also, this approach only works with real/floating-point numbers. If you want integers, you got a combinational problem on your hands.