代码之家  ›  专栏  ›  技术社区  ›  Andre

n参数内的随机数

  •  2
  • Andre  · 技术社区  · 15 年前

    假设我有一个数字,比如100,我想生成5个不同的随机数,但它们都必须加起来为100,我该怎么做?(最好用PHP。对于数学奇才/统计学家来说,我不需要真正的随机数,而是一些看起来随机的数字)。

    所以这个函数,会产生这样的结果:

    5,51,9,18,19=100 34、52、3、7、4=100

    等等。

    理想情况下,需要5100个并产生其余的:

    生成域(100,5)

    2 回复  |  直到 15 年前
        1
  •  4
  •   Dr. belisarius    14 年前
    a = RandomInteger[{1, 96}]
    b = RandomInteger[{1, 97 - a}]
    c = RandomInteger[{1, 98 - a - b}]
    d = RandomInteger[{1, 99 - a - b - c}]
    e = 100 - a - b - c - d
    

    样品:

    {34,25,26,3,12,Sum =,100}
    
    {90,5,1,1,3,Sum =,100}
    
    {29,16,21,9,25,Sum =,100}
    
    {4,13,71,10,2,Sum =,100}
    

    当然,这些数字分布不均。

    编辑

    这里有一个更均匀的分布:

    a = RandomInteger[{1, 20}];
    b = RandomInteger[{1, 40 - a}];
    c = RandomInteger[{1, 60 - a - b}];
    d = RandomInteger[{1, 80 - a - b - c}];
    e = 100 - a - b - c - d;
    

    输出:

    {5,33,2,8,52,Sum =,100}
    {14,9,50,5,22,Sum =,100}
    {3,23,12,34,28,Sum =,100}
    {1,16,4,5,74,Sum =,100}
    {6,28,6,9,51,Sum =,100}
    {11,25,7,1,56,Sum =,100}
    {4,34,12,18,32,Sum =,100}
    {6,13,25,26,30,Sum =,100}
    {8,27,14,5,46,Sum =,100}
    {17,13,23,25,22,Sum =,100}
    

    以下是数字的频率:

    alt text

    编辑

    也许是更好的选择:

    a = Max[#, 1] & /@ Evaluate[RandomInteger[{1, 20}, 5] - 1];
    b = 100 - Total@a;
    c = Mod[b, 5];
    d = (b - c)/ 5;
    a = a + d + {c, 0, 0, 0, 0};
    

    分发: alt text

    编辑

    在 Mathematica 您可以轻松地生成所有5个元组,这些元组加起来为100:

    IntegerPartitions[100, {5}]
    

    有38225种不同的动物,不计算排列

    Length@IntegerPartitions[100, {5}]
    (* -> 38225 *)
    

    五元组中每个数的频率为:

    Histogram@Flatten@IntegerPartitions[100, {5}]
    

    enter image description here

    如果考虑排列,曲线非常相似:

    t = Tally@Flatten@(Permutations[#] & /@ IntegerPartitions[100, {5}]); 
    ListLinePlot@SortBy[t, #[[1]] &][[All, 2]]
    

    enter image description here

        2
  •  1
  •   Brad Christie    15 年前

    沿着贝利萨留斯的路线:

      // Generates an array (of $elements elements) full of random numbers whose
      // total is equal to the $total_sum
      function generate_random_array($elements = 5, $total_sum = 100)
      {
        // build result array
        $result = Array();
    
        // iterate over all elements (except for the last, last will be the delta)
        for ($_ = 0; $_ < ($elements - 1); $_++)
        {
          // typical low value (non-zero)
          $low_value = 1;
    
          // high value, skewed to have high results first
          //$high_value = ($total_sum - ($elements - $_) - array_sum($result));
          // high value, non-skewed
          $step = (int)floor($total_sum / $elements); // give "not-to-exceed" ranges
          $high_value = (($_ + 1) * $step) - array_sum($result);
    
          // produce the result and add it
          $result[] = rand($low_value,$high_value);
          //$result[] = rand(1,65535) % $high_value + 1; // alternate to make rand a little smoother
        }
    
        // add the final result as the remainder
        $result[] = $total_sum - array_sum($result);
    
        // now return it
        return $result;
      }
    
    推荐文章