bentools/picker
Composer 安装命令:
composer require bentools/picker
包简介
Helps you pick a random item with weight management.
README 文档
README
Picker
A PHP library for randomly picking items from collections and generating random numbers with advanced options like seeding, weighting, and more.
Features
- Pick random items from collections
- Generate random numbers with optional seeding
- Support for weighted item selection
- Option to allow or prevent duplicates in selections
- Consistent results with seeded randomization
Quick Start
Picking Items from a Collection
Basic usage
use BenTools\Picker\Picker; $items = ['apple', 'banana', 'cherry']; $picker = Picker::fromItems($items); $randomItem = $picker->pick(); // Returns a random item from the array
Prevent duplicates
It will avoid, as much as possible, picking the same item twice in a row.
If all items have been picked, it will cycle through them again.
use BenTools\Picker\Picker; use BenTools\Picker\ItemPicker\ItemPickerOptions; $options = new ItemPickerOptions(allowDuplicates: false); $picker = Picker::fromItems($items, $options); $randomItem = $picker->pick(); // Will cycle through all items before repeating
Direct Number Generation
use function BenTools\Picker\random_int; // Alternative to PHP's built-in random_int with optional seeding $randomNumber = random_int(1, 100); // Behaves like PHP's random_int() $seededNumber = random_int(1, 100, 12345); // Deterministic output for given seed
Advanced Usage
Item Picker Options
use BenTools\Picker\Picker; use BenTools\Picker\ItemPicker\ItemPickerOptions; use BenTools\Picker\ItemPicker\Algorithm\Algorithm; $options = new ItemPickerOptions( algorithm: Algorithm::RANDOM, // Selection algorithm defaultWeight: 1, // Default weight for items allowDuplicates: true, // Whether to allow the same item to be picked multiple times maxLoops: PHP_INT_MAX, // Maximum number of times to loop through all items seed: 12345, // Optional seed for reproducible results // weights: $customWeightProvider // Custom weight provider implementation ); $picker = Picker::fromItems(['apple', 'banana', 'cherry'], $options);
Use Cases
- Randomizing elements in games
- Implementing A/B testing
- Creating fair selection mechanisms
- Generating random but reproducible test data
- Implementing weighted random selection for various algorithms
Installation
This library requires PHP 8.2+.
composer require bentools/picker
Tests
./vendor/bin/pest
See also
bentools/picker 适用场景与选型建议
bentools/picker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.6k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 08 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bentools/picker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bentools/picker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 12.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-28