【问题标题】:Generate N numbers whose product is 1 and more generally when the product is P?当乘积为 P 时,生成 N 个乘积为 1 和更普遍的数?
【发布时间】:2014-08-04 11:52:05
【问题描述】:

我想生成 N 个随机点,其乘积为 1。

我在 MATALB 中是这样做的:

N_=10;
x1_=rand(1, N_);
p_=prod(x1_);
x_=x1_;
x_(end)=x1_(end)/p_;
prod(x_);

有没有简单的(其他方式)?那么当 P ~= 1 时呢?

谢谢。

【问题讨论】:

标签: matlab random


【解决方案1】:

一种方法是使用log(1) = 0a*b = exp(log(a+b)) 的事实。

x = rand(N,1);
norm_log_x = log(x)-mean(log(x));
norm_x = exp(norm_log_x);
norm_x =

    2.6058
    2.7166
    0.6396
    1.3958
    1.0874
    1.5147
    0.7660
    0.1977
    1.2618
    0.5028

prod(norm_x)
ans =
    1.0000

要获取其他1 的值,您可以这样做:

P = 7;
norm_log_x = log(x)-mean(log(x))+log(P)/length(x);
norm_x = exp(norm_log_x);
prod(norm_x)    
ans =
    7.0000

【讨论】:

    【解决方案2】:

    在您的情况下,最后一个数字具有不同的分布。假设您希望所有数字的分布相同:

    N_=10;
    x1_=rand(1, N_);
    p_=prod(x1_);
    x_=x1_./(p_.^(1/N_))
    

    【讨论】:

      【解决方案3】:

      Lognormal distribution 具有产品再次对数正态分布的属性。这也意味着,当您可以进行最后一次“修复”时,将无法找到用于此“分布修剪”的数字。

      【讨论】:

        【解决方案4】:

        我现在无法访问 MATLAB,但你不能这样做..

        n      = 10;
        x_sum  = 1;
        x_rand = rand(x_sum, n);
        x_rand = x_rand ./ sum(x_rand);
        

        这种方式给出了一个随机列表,如果您不希望总和为 1,您可以更改 x_sum。如果您想要均匀分布或其他一些均匀分布,您可以将 rand() 替换为不同的内置在函数中。

        编辑: 我会快速运行以仔细检查我的代码是否正确。

        sum(x_rand);

        【讨论】:

          猜你喜欢
          • 2020-11-12
          • 1970-01-01
          • 2012-12-13
          • 1970-01-01
          • 2018-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多