以下是一个用PHP实现的概率函数,它接收一个概率值作为参数,并根据该概率随机返回true(中)或false(不中):

<?php
/**
 * 根据给定的概率值返回布尔结果
 * @param float $probability 概率值,范围0-100(包含)
 * @return bool true表示中,false表示不中
 */
function probabilityResult(float $probability): bool {
    // 确保概率在有效范围内
    $probability = max(0, min(100, $probability));
    
    // 生成1到100之间的随机整数
    $randomNumber = mt_rand(1, 100);
    
    // 判断是否在概率范围内
    return $randomNumber <= $probability;
}

// 示例使用
$chance = 30; // 30%的概率
$result = probabilityResult($chance);
echo $result ? "中了!" : "未中";
?>

这个函数的工作原理是:

  1. 首先将传入的概率值限制在0-100的范围内
  2. 使用mt_rand()函数生成一个1到100之间的随机整数(mt_rand()rand()更适合用于概率计算)
  3. 如果生成的随机数小于或等于传入的概率值,则返回true(中),否则返回false(不中)

例如,如果你传入30作为概率值,那么函数有30%的概率返回true,70%的概率返回false

下面提供几种不同实现思路的概率函数,各有特点和适用场景:

1. 浮点数精度算法(支持0-1概率)

function floatProbability(float $probability): bool {
    if ($probability <= 0) return false;
    if ($probability >= 1) return true;
    
    // 归一化到0-1区间
    $normalized = max(0, min(1, $probability));
    return (random_int(0, PHP_INT_MAX) / PHP_INT_MAX) < $normalized;
}

特点

  • 直接支持0.001%这样的极小概率
  • 使用random_int()生成高安全性随机数
  • 通过浮点数比较实现高精度概率

2. 权重随机算法(适合多选项概率分布)

/**
 * 权重随机选择
 * @param array $weights ['选项A' => 30, '选项B' => 70]
 * @return string|false 选中的选项键名
 */
function weightedRandom(array $weights) {
    $total = array_sum($weights);
    $rand = random_int(1, $total);
    
    foreach ($weights as $key => $weight) {
        $rand -= $weight;
        if ($rand <= 0) {
            return $key;
        }
    }
    
    return false;
}

// 示例:30%概率返回true,70%返回false
$result = weightedRandom([true => 30, false => 70]) === true;

特点

  • 适合实现复杂概率分布(如抽奖池)
  • 所有选项权重之和代表100%概率
  • 通过区间划分实现权重选择

3. 模拟概率波动算法(游戏常用)

class ProbabilityShaker {
    private float $baseProbability;
    private float $currentChance = 0;
    private float $growthRate = 0.1;
    
    public function __construct(float $baseProbability, float $growthRate = 0.1) {
        $this->baseProbability = $baseProbability;
        $this->growthRate = $growthRate;
    }
    
    public function try(): bool {
        $chance = $this->baseProbability + $this->currentChance;
        $result = (random_int(1, 100) <= $chance);
        
        if ($result) {
            $this->currentChance = 0; // 触发后重置
        } else {
            $this->currentChance += $this->growthRate; // 未触发则增加下次概率
        }
        
        return $result;
    }
}

// 示例:基础30%概率,每次失败后增加10%补偿
$shaker = new ProbabilityShaker(30, 10);
$result = $shaker->try();

特点

  • 实现"保底机制",多次失败后概率递增
  • 适合游戏抽卡、暴击率等场景
  • 通过状态累积改变实际概率

4. 时间相关概率算法

function timeBasedProbability(float $probability): bool {
    // 获取当前时间的毫秒级哈希值
    $timeHash = crc32(microtime(true));
    $normalizedHash = ($timeHash % 10000) / 100; // 转换为0-100的浮点数
    
    return $normalizedHash <= $probability;
}

特点

  • 相同时间点调用结果相同(伪随机)
  • 适合需要时间稳定性的概率系统
  • 可用于限时活动概率调整

应用场景建议:

  • 普通概率判断:使用原始答案的整数区间法
  • 高精度概率:使用浮点数精度算法
  • 多选项随机:使用权重随机算法
  • 游戏机制:使用概率波动算法
  • 时间相关系统:使用时间哈希算法

标签: none

添加新评论