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 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害