如何生成 5 个总和为 100 的随机数 [重复]

2024-03-27

你知道一种将整数分成 5 组的方法吗? 每组总数必须是随机的,但总数必须等于固定数字。

例如我有“100”我想把这个数字分成

1- 20
2- 3
3- 34
4- 15
5- 18

编辑:我忘了说,是的,平衡是一件好事。我想这可以通过创建一个 if 语句来阻止任何超过 30 个实例的数字来完成。


我对这里的一些答案有稍微不同的方法。我根据您想要求和的项目数量创建一个宽松的百分比,然后随机加减 10%。

然后我执行 n-1 次(n 是迭代总数),这样就得到了余数。余数就是最后一个数字,它本身并不是真正的随机数,但它基于其他随机数。

效果很好。

/**
 * Calculate n random numbers that sum y.
 * Function calculates a percentage based on the number
 * required, gives a random number around that number, then
 * deducts the rest from the total for the final number.
 * Final number cannot be truely random, as it's a fixed total,
 * but it will appear random, as it's based on other random
 * values.
 * 
 * @author Mike Griffiths
 * @return Array
 */
private function _random_numbers_sum($num_numbers=3, $total=500)
{
    $numbers = [];

    $loose_pcc = $total / $num_numbers;

    for($i = 1; $i < $num_numbers; $i++) {
        // Random number +/- 10%
        $ten_pcc = $loose_pcc * 0.1;
        $rand_num = mt_rand( ($loose_pcc - $ten_pcc), ($loose_pcc + $ten_pcc) );

        $numbers[] = $rand_num;
    }

    // $numbers now contains 1 less number than it should do, sum 
    // all the numbers and use the difference as final number.
    $numbers_total = array_sum($numbers);

    $numbers[] = $total - $numbers_total;

    return $numbers;
}

This:

$random = $this->_random_numbers_sum();
echo 'Total: '. array_sum($random) ."\n";
print_r($random);

Outputs:

Total: 500
Array
(
    [0] => 167
    [1] => 164
    [2] => 169
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何生成 5 个总和为 100 的随机数 [重复] 的相关文章

  • 在 while 循环内查询可以吗?

    我在一个数据库中有两个表 我正在查询第一个表限制 10 然后循环结果 在 while 循环内 我使用第一个查询中的数据作为参数再次执行另一个查询 以下是该脚本的示例
  • PHP UTF-8 配置

    我正在使用 PHP 5 3 5 配置 Apache 2 2 17 服务器 我的目标是创建一个默认为内容类型的干净配置UTF 8 php ini default charset UTF 8 default mimetype applicati
  • Unity - 在生成时获取随机颜色

    我有一个小问题 我想在我的场景中生成四边形 它们都应该有红色或绿色作为材质 但 Random Range 函数只能是 int 我该如何解决它 void SpawningSquadsRnd rndColor 0 Color red rndCo
  • 如何使用 Vim 和 Eclipse 提高 PHP5.3 项目的工作效率(可能使用 Eclim 与 Zend Studio 集成)

    在开发应用程序时 我总是努力提高生产力 在过去的几年里 我可以说我在提高生产力方面取得了良好的进展 但我仍然发现我需要更多的工具或方法 我需要更快地编辑并学习盲打 目前正在过程中 因此我发现需要转向 Vim 现在已经一年多了 我已经放弃了
  • Magento Connect Manager 和 ftp 写入文件权限

    将我的网站从开发域转移到实时域后 我尝试使用 magento 连接管理器安装扩展 但是我不断收到 CONNECT ERROR Please check for sufficient ftp write file permissions Yo
  • 如何在Wamp服务器中启用SSL?

    我尝试在网上搜索它 但我很困惑 我没有得到任何澄清 逐步教程 http blog facilelogin com 2008 07 enabling ssl on wamp html 从链接复制 在 WAMP 上启用 SSL 本分步指南介绍了
  • 某些表格后的分页符

    我的问题是 我有一个页面 其中包含几个要打印的 html 表格 有些表有很多行 有些则没有 我想要做的是将第一个和第二个表 大表 打印在单独的页面中 其余表 小表 每页打印两个 如何在我想要的位置放置分页符 我试过 但这会在每个表格后面添加
  • 数组和关联数组合并

    如何实现第三个数组 合并Array1和Array2 在 PHP 中做到这一点的最佳方法是什么 非常感谢 Array2 具有类似索引 键 即 Array1 中 id 的关联值 Array1 Array 0 gt Array id gt 56
  • Laravel 6:尚未设置外观根

    经过一段时间的努力 我已将我的网站从 Laravel 5 8 迁移到 Laravel 6作曲家更新我在网站上遇到此错误 并且仅使用命令PHP工匠 PHP Fatal error Uncaught RuntimeException A fac
  • 使用 Jquery Easyui 将数据网格导出到 Excel

    我是 json 新手 我使用 php 从 mysql 表生成了 jason 数据 并希望将生成的 json 导出为 xls 格式 考试导出 php
  • 按给定日期获取上周一和下周一的日期[重复]

    这个问题在这里已经有答案了 我们如何通过提供的日期获取上一周星期一日期和下周星期一日期 示例 if date 2015 04 08 年月日格式 然后函数返回 上周一日期 2015 03 30 下周一日期 2015 04 13 echo Ne
  • 使用 Laravel Fluent 查询生成器从多个表中进行选择

    我正在重写一些 PHP MySQL 来与 Laravel 一起使用 我想做的一件事是使数据库查询更加简洁使用 Fluent 查询生成器 http laravel com docs database fluent但我有点迷失 SELECT p
  • PHP 中的多行字符串文字

    考虑 xml l xml vv echo xml 这将回响vv 为什么以及如何为诸如此类的事情执行多行字符串文字简单XML https en wikipedia org wiki SimpleXML etc Well xml l vv Wo
  • 我可以在 Laravel 5.2 中创建一个继承自 User 的新类吗?

    我对 Laravel 还很陌生 使用的是迄今为止的最新版本 5 2 因此我遇到了以下困境 我知道 Laravel 附带了一个User开箱即用的类 但我想开发一个系统 在其中我可以有另外两种类型的用户 称为Researcher and Adm
  • CMS:将自定义页面存储为文件或 MySQL 数据库中?

    我正在 PHP 中创建一个自定义 CMS 从头开始编写 并且想知道是否应该将用户创建的页面存储为文件或存储在 MySQL 数据库中 内容全部是 HTML 代码 至少目前是这样 我无法决定该做什么 因为用 php 编写文件似乎存在安全风险 并
  • 如何在 php 中为每个其他函数调用自动调用函数

    Class test function test1 echo inside test1 function test2 echo test2 function test3 echo test3 obj new test obj gt test
  • 返回早期概念在 PHP 中有何用处

    我已经在以下链接中了解了最佳实践https pear php net manual en standards bestpractices php https pear php net manual en standards bestprac
  • 如何将事件插入为 - Out Office

    我目前正在使用 Google Calendar API 并尝试在我的谷歌日历中插入新的 外出 事件 我使用以下代码插入事件 client getClient service new Google Service Calendar clien
  • 如何在刀片模板中通过引用 @include 来传递变量?

    在 Laravel 4 2 设置中 我在模板中有一个变量 我希望在多个包含之间共享该变量 主刀 This is the variable include header lt in header blade I often use tabin
  • Laravel中with()和compact()有什么区别

    功能有什么区别with and compact 在 Laravel 中这两个例子 示例1 return View make books index gt with booksList booksList 示例2 return View ma

随机推荐