PHPackages                             greensea/o1-weighted-random - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. greensea/o1-weighted-random

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

greensea/o1-weighted-random
===========================

Weighted random item picker, in O(1) time complexity

08PHP

Since Jun 16Pushed 3y ago1 watchersCompare

[ Source](https://github.com/greensea/o1-weighted-random)[ Packagist](https://packagist.org/packages/greensea/o1-weighted-random)[ RSS](/packages/greensea-o1-weighted-random/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

o1-weighted-random
==================

[](#o1-weighted-random)

Weighted random item picker, in O(1) time complexity

Usage
-----

[](#usage)

```
$c = new O1WeightedRandom();
$c->add("A", 1);    // Item A with weight 1
$c->add("B", 2);    // Item B with weight 2
$c->add("C", 3);    // Item C with weight 3
$c->add("D", 4);    // Item D with weight 4

echo $c->pick();
/// most probability output D

/// You can add more items
$c->add("E", 10)
echo $c->pick();
/// Now the most probable output is E

```

You can verify the item is choosen by weight.

```
$result = ['A' => 0, 'B' => 0, 'C' => 0, 'D' => 0];
for ($i = 0; $i < 10000; $i++) {
    $key = $c->pick();
    $result[$key] += 1;
}
print_r($result);
// Array
// (
//     [A] => 10001
//     [B] => 20116
//     [C] => 29976
//     [D] => 39907
// )

```

About O(1) Time Complexity
--------------------------

[](#about-o1-time-complexity)

The `pick()` is usually O(1) time, but the first call to `pick()` is O(n) time complexity. The library need some time to build the data structure at first picking action.

If you add new item after first `pick()` action, the library need and other O(n) time to rebuild the data structure.

This library is base on the algorithm published here:

You can learn more details of the algorithm here:

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity25

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/135ae539aa14fcf702ba4f81b38730d456ba96a7a31fef91c4a0abefa9e00717?d=identicon)[greensea](/maintainers/greensea)

---

Top Contributors

[![greensea](https://avatars.githubusercontent.com/u/619187?v=4)](https://github.com/greensea "greensea (5 commits)")

### Embed Badge

![Health badge](/badges/greensea-o1-weighted-random/health.svg)

```
[![Health](https://phpackages.com/badges/greensea-o1-weighted-random/health.svg)](https://phpackages.com/packages/greensea-o1-weighted-random)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M683](/packages/barryvdh-laravel-ide-helper)[breadlesscode/neos-nodetypes-folder

Folder nodetype for Neos CMS

1088.5k1](/packages/breadlesscode-neos-nodetypes-folder)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
