Skip to content

Provides an O(1) weighted random algorithm implementation #1184

@jizhuozhi

Description

@jizhuozhi

Is your feature request related to a problem? Please describe.

The current weighted random algorithm uses CDF (cumulative distribution function), which calculates the probability prefix sum ($F_X(x) = P(X \le x)$) of position $x$, then generates a random number between 0-1, and selects the upper bound of this random number in the CDF. The preprocessing time complexity of CDF is O(n), and the selection requires O(logn) time complexity.

Describe the solution you'd like

In fact, there is a weighted random algorithm with preprocessing time complexity O(n) and selection time complexity O(1), called the alias method. This article http://www.keithschwarz.com/darts-dice-coins/ describes how this algorithm is implemented, I hope kitex can support this algorithm.

Describe alternatives you've considered

Because this algorithm is relatively simple, I think it will be a good task for newcomers

Additional context

CDF: https://en.wikipedia.org/wiki/Cumulative_distribution_function
Alias Method:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions