prd

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package prd 提供伪随机概率补偿(Pseudo-Random Distribution)算法。

解决的问题:真随机在小样本下体验极差——25% 暴击率的角色可能连续 10 刀不暴击, 也可能连续 3 刀暴击,玩家感知不到"公平"。PRD 通过动态调整每次触发概率,使得:

  • 连续不触发越久,下次触发概率越高(保底感);
  • 一旦触发立即重置,避免连续触发(避免爆发不公);
  • 长期触发率精确等于标称概率 P。

数学原理:

第 N 次尝试的触发概率 = N × C
其中 C 是一个由标称概率 P 反解出的常数(满足期望触发次数 = 1/P)。
当 N×C ≥ 1 时保证触发(硬保底)。

典型应用:MOBA 暴击率、闪避率、触发型被动技能、怪物掉落。

PRD 与 loot.Puller 的区别:Puller 是硬保底计数器(到次数必出),PRD 是 概率递增的"软保底"——体感更自然,且严格保持标称概率。

Roller 有内部状态,非并发安全(每角色一个,或自行加锁)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CFromP

func CFromP(p float64) float64

CFromP 返回标称概率 P 对应的 PRD 常数 C。P 应在 (0, 1] 之间。 P <= 0 返回 0;P >= 1 返回 1。

func MaxN

func MaxN(p float64) int

MaxN 返回标称概率 P 下的最大保底次数(N×C ≥ 1 时必触发)。

Types

type Option

type Option func(*Roller)

Option 配置 Roller。

func WithRand

func WithRand(r *rand.Rand) Option

WithRand 指定随机源(用于可复现测试)。默认用 math/rand/v2 全局源。

type Roller

type Roller struct {
	// contains filtered or unexported fields
}

Roller 是一个有状态的 PRD 判定器。每个角色/技能持有一个实例。 非并发安全。

func New

func New(p float64, opts ...Option) *Roller

New 创建 PRD 判定器。p 为标称触发概率,如 0.25 表示 25% 暴击率。

func (*Roller) Count

func (r *Roller) Count() int

Count 返回当前连续未触发次数。

func (*Roller) NextProb

func (r *Roller) NextProb() float64

NextProb 返回下一次判定的触发概率(用于 UI 提示)。

func (*Roller) Reset

func (r *Roller) Reset()

Reset 重置内部计数(如角色死亡/回合切换时调用)。

func (*Roller) Roll

func (r *Roller) Roll() bool

Roll 执行一次判定:返回是否触发。触发后内部计数重置。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL