dongyan9838 2016-12-23 14:48
浏览 42
已采纳

相对于数字的概率

I would like to make a roulette for my PHP site, so I need to set the probability of the items. So I created an array with the probability of the different items:

(the higher the number the higher the chance)

$porpList = [
0 => [
    "prop" => 10000,
    "css" => "gray"
],
1 => [
    "prop" => 1000,
    "css" => "green"
],
2 => [
    "prop" => 100,
    "css" => "blue"
],
3 => [
    "prop" => 10,
    "css" => "violet"
],
4 => [
    "prop" => 1,
    "css" => "yellow"
],
5 => [
    "prop" => 0.1,
    "css" => "orange"
]];

Now comes the difficult part: The User with a higher Level should have a greater chance to get better Items and a lower chance to get trash Items, so with level 1 the probability would be:

10000
1000
100
10
1
0.1

And with Level 50 the probability should be something like this:

1
1
10000
1000
100
10

Logically, I could create a table in which all the possibilities for each level are listed, but I would like to solve this with a formula or similar.

Does anyone know a way to do this?

  • 写回答

2条回答 默认 最新

  • duanke6057 2016-12-23 15:06
    关注

    If you want to implement an actual algorithm to determine probability. I'm out.

    If you want to heighten the chances of people rolling better stuff, you could use code similar to this;

    $rarities = [
        [
            'prop'  => 100000,
            'css'   => 'gray'
        ],
        [
            'prop'  => 10000,
            'css'   => 'green'
        ],
        [
            'prop'  => 1000,
            'css'   => 'blue'
        ],
        [
            'prop'  => 100,
            'css'   => 'violet'
        ],
        [
            'prop'  => 10,
            'css'   => 'yellow'
        ],
        [
            'prop'  => 1,
            'css'   => 'orange'
        ]
    ];
    $level = 50;
    $maxProp = (100000/$level);
    $won = 'gray';
    for ($i=0;$i<20;$i++)
    {
        $roll = random_int(0, $maxProp);
        foreach ($rarities as $rarity)
        {
            if ($rarity['prop'] < $roll)
            {
                break;
            }
            $won = $rarity['css'];
        }
        echo '<span style="color: '.$won.'; font-weight: bold; font-familiy: Arial;">'.$roll.'</span><br/>';
    

    The higher the level, the higher chances are something good will 'drop'.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致