13

我需要生成 3 个随机数,其数量等于 1。

我的实现不支持统一分布。:(

4

9 回答 9

27

只需获取 3 个随机数,然后计算一个因子 1 / [您的数字之和]。最后将每个随机数乘以该因子。总和将为 1。

于 2011-04-06T09:01:17.483 回答
11

这实际上是一个棘手的问题。首先:
Daren的解并不统一,因为它不支持两个数 > 1/3。
假设“选择一个随机数”来自均匀分布, Simen的解决方案并不统一,但这有点微妙。它至少在变量之间是对称的(即 [a, b, c] 的概率与它的任何排列的概率相同),但它非常有利于更接近 (1/3, 1/3, 1/ 3)。通过观察极端情况来这样想:(1/3, 1/3, 1/3) 可能来自任何 (a, a, a),其中 a 的范围是 0 到 1。 (1, 0, 0),一个同样有效的三元组,必须来自 (1, 0, 0)。

一种解决方案:加 1 的一组正数在三个空间中形成一个等边三角形,坐标为 (1,0,0)、(0,1,0)、(0,0,1)。将其扩展到平行四边形——例如,通过添加一个点 (1,1,-1) 作为第四个点。这个双倍的区域 - 将第二个区域映射到第一个区域,以便在这个平行四边形中选择一个随机点就足够了。

平行四边形可以通过 (0,0,1) + A(1,0,-1) + B (0,1,-1) 均匀采样,其中 A 和 B 的范围均匀地从 0 到 1。

-一种

于 2011-07-20T08:49:28.787 回答
3

生成两个介于 0 和 1 之间的随机数。将它们除以 3。第三个是 1 和两个随机三分之二的差:

void Main()
{
    Random r = new Random();
    double d1 = r.NextDouble() / 3.0;
    double d2 = r.NextDouble() / 3.0;
    double d3 = 1.0 - d1 - d2;
    System.Console.WriteLine(d1);
    System.Console.WriteLine(d2);
    System.Console.WriteLine(d3);
    System.Console.WriteLine(d1 + d2 + d3);
}

这在 LINQPad 中输出以下内容:

0.0514050276878934
0.156857372489847
0.79173759982226
1
于 2011-04-06T09:00:36.937 回答
1

更新

  1. 创建一个由 3 个随机数组成的 vector3
  2. 规范化向量
于 2011-04-06T09:00:53.880 回答
1

Marnix 的回答略有不同:

  1. a从 [0,1]生成一个随机数
  2. 生成两个随机数。x来自 [0,a] 和y来自 [a,1]
  3. 将结果设置为x, y-x,1-y
于 2011-04-06T09:16:00.857 回答
1

有一种简单的方法可以做到这一点,但您需要能够生成统一的随机数。

让 X 在 (0,2/3) 上均匀。如果 X < 1/3,让 Y = X + 1/3。否则让 Y = X - 1/3。令 Z = 1 - X - Y。

在此设置下,X、Y 和 Z 的总和为 1,它们都将具有相同的均匀 (0, 2/3) 边际分布,并且所有三个成对相关性均为 -(1/2)。

于 2013-10-03T18:50:53.920 回答
0

2/2方法:

  • 创建一个 0 到 1 的随机数列表;缩放到总数
  • 将列表从小到大排序
  • 通过测量第一个列表中每个元素之间的空间来创建一个新列表
  • 对新列表中的每个元素进行四舍五入
  • 替换第一个元素以考虑浮点数

对不起,我不知道 C# 这是它在 python 中的样子:

import random
import time

PARTS       = 5
TOTAL       = 10
PLACES      = 3

def random_sum_split(parts, total, places):


    a = [0.0, total]
    for i in range(parts-1):
        a.append(random.random()*total)
    a.sort()
    b = []
    for i in range(1,(parts+1)):
        b.append(a[i] - a[i-1])
    if places != None:    
        b = [round(x, places) for x in b]  
    c = b[-(parts-1):]
    d = total - sum(c)
    if places != None:
        d = round(d, places)
    c.insert(0, d)

    log(a)
    log(b)
    log(c)
    log(d)

    return c

def tick():

    if info.tick == 1:

        start = time.time()

        alpha = random_sum_split(PARTS, TOTAL, PLACES)

        log('********************')
        log('***** RESULTS ******')
        log('alpha: %s' % alpha)
        log('total: %.7f' % sum(alpha))
        log('parts: %s' % PARTS)
        log('places: %s' % PLACES)

        end = time.time()  

        log('elapsed: %.7f' % (end-start))

产量:

Waiting...
Saved successfully.
[2014-06-13 00:01:00] [0.0, 1.3005056784596913, 3.0412441135728474, 5.218388755020509, 7.156425483589107, 10]
[2014-06-13 00:01:00] [1.301, 1.741, 2.177, 1.938, 2.844]
[2014-06-13 00:01:00] [1.3, 1.741, 2.177, 1.938, 2.844]
[2014-06-13 00:01:00] 1.3
[2014-06-13 00:01:00] ********************
[2014-06-13 00:01:00] ***** RESULTS ******
[2014-06-13 00:01:00] alpha: [1.3, 1.741, 2.177, 1.938, 2.844]
[2014-06-13 00:01:00] total: 10.0000000
[2014-06-13 00:01:00] parts: 5
[2014-06-13 00:01:00] places: 3
[2014-06-13 00:01:00] elapsed: 0.0036860
于 2014-09-05T01:12:18.143 回答
0

1/2方法:

  • 创建一个随机数列表,每个 0 到 1 的长度为 PARTS。
  • 总结列表
  • 将每个元素除以总和
  • 对每个元素进行四舍五入
  • 通过编辑第一个元素来考虑浮点数学

抱歉不知道 C#,这里是 python:

import random
import time

PARTS       = 5
TOTAL       = 10
PLACES      = 3

def random_sum_split(parts, total, places):

    a = []
    for n in range(parts):
        a.append(random.random())
    b = sum(a)
    c = [x/b for x in a]    
    d = sum(c)
    e = c
    if places != None:
        e = [round(x*total, places) for x in c]
    f = e[-(parts-1):]
    g = total - sum(f)
    if places != None:
        g = round(g, places)
    f.insert(0, g)

    log(a)
    log(b)
    log(c)
    log(d)
    log(e)
    log(f)
    log(g)

    return f   

def tick():

    if info.tick == 1:

        start = time.time()

        alpha = random_sum_split(PARTS, TOTAL, PLACES)

        log('********************')
        log('***** RESULTS ******')
        log('alpha: %s' % alpha)
        log('total: %.7f' % sum(alpha))
        log('parts: %s' % PARTS)
        log('places: %s' % PLACES)

        end = time.time()  

        log('elapsed: %.7f' % (end-start))

产量:

Waiting...
Saved successfully.
[2014-06-13 00:01:00] [0.33561018369775897, 0.4904215932650632, 0.20264927800402832, 0.118862130636748, 0.03107818050878819]
[2014-06-13 00:01:00] 1.17862136611
[2014-06-13 00:01:00] [0.28474809073311597, 0.41609766067850096, 0.17193755673414868, 0.10084844382959707, 0.02636824802463724]
[2014-06-13 00:01:00] 1.0
[2014-06-13 00:01:00] [2.847, 4.161, 1.719, 1.008, 0.264]
[2014-06-13 00:01:00] [2.848, 4.161, 1.719, 1.008, 0.264]
[2014-06-13 00:01:00] 2.848
[2014-06-13 00:01:00] ********************
[2014-06-13 00:01:00] ***** RESULTS ******
[2014-06-13 00:01:00] alpha: [2.848, 4.161, 1.719, 1.008, 0.264]
[2014-06-13 00:01:00] total: 10.0000000
[2014-06-13 00:01:00] parts: 5
[2014-06-13 00:01:00] places: 3
[2014-06-13 00:01:00] elapsed: 0.0054131
于 2014-09-05T01:15:46.493 回答
0

基于@Simen 和@Daren Thomas 的答案,这里是一个服务函数,它返回一个具有统一随机值的双精度数列表,您可以在其中指定您想要多少个数字、总和以及数字上的位数:

        public static List<double> GetListOfRandomDoubles(int countOfNumbers, double totalSum, int digits)
        {
            Random r = new Random();

            List<double> randomDoubles = new List<double>();
            double totalRandomSum = 0; 

            for (int i = 0; i < countOfNumbers; i++)
            {
                double nextDouble = r.NextDouble();
                randomDoubles.Add(nextDouble);
                totalRandomSum += nextDouble;
            }

            double totalFactor = 1 / totalRandomSum;
            totalFactor = totalFactor * totalSum;

            for (int i = 0; i < randomDoubles.Count; i++)
            {
                randomDoubles[i] = randomDoubles[i] * totalFactor;
                randomDoubles[i] = Math.Round(randomDoubles[i], digits);
            }

            double currentRandomSum = 0;
            randomDoubles.ForEach(x => currentRandomSum += x);
            randomDoubles[0] += totalSum - currentRandomSum;

            return randomDoubles;
        }

用法:

        // Get list of 7 random doubles that sum to 100, with up to 2 digits on each number
        List<double> randomDoubles = GetListOfRandomDoubles(7, 100, 2);

回报:

12.25, 19.52, 15.49, 16.45, 1.92, 13.12, 21.25

于 2020-05-12T08:21:05.340 回答