robier/probability-checker 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

robier/probability-checker

最新稳定版本:v1.0.2

Composer 安装命令:

composer require robier/probability-checker

包简介

Probability Checker is a simple library that helps you to check the probability of an event.

README 文档

README

codecov

ProbabilityChecker

Pretty simple probability implementation that can be used for A/B testing. If you have some feature that you want to run on 20% of the requests. This is right tool for you as you do not need to define how many requests there will be in total.

If you provide 0 as a parameter, it will always return false, if you provide 100, it will always return true. Everything in between will be around the number you provided.

Note: This method is not exact but approximation, so if you want 20%, you will get 21%, 23% maybe 18%, but at the end it will be around 20% (the greater number of request, more accurate will be the percentage).

Installation

This library can be installed via composer:

composer require robier/probability-checker

Sample usage

<?php

$probability = new \Robier\ProbabilityChecker(30);

$skipped = 0;
$executed = 0;
for($i = 0; $i < 1000000; $i++) {
    if ($probability->roll()) {
        $executed++;
    } else {
    	$skipped++;
    }
}

$percentageAtTheEndSkipped = round($skipped / ($executed + $skipped), 6) * 100;
$percentageAtTheEndExecuted = round(100 - $percentageAtTheEndSkipped, 6);

echo "Executed $executed and Skipped $skipped \n";
echo "Executed $percentageAtTheEndExecuted% and Skipped $percentageAtTheEndSkipped%";

1st run

Skipped 699978 and Executed 300022 
Skipped 69.9978% and Executed 30.0022%

2nd run

Skipped 699813 and Executed 300187 
Skipped 69.9813% and Executed 30.0187%

3rd run

Skipped 700351 and Executed 299649 
Skipped 70.0351% and Executed 29.9649%

From this you can see that the percentage is not exact, but it is around 30% as we defined.

Methods

We have few methods in the class:

Name Description
roll() Like rolling the dice, depending on provided percentage it will be true or false
isCertian() If percentage is 100 or more, this method will return true
isImpossible() If percentage is 0 or less, this method will return true
isPossible() If percentage is between 0 and 100 (both excluded), this method will return true
::always() Factory method, that will create new instance with 100%
::never() Factory method, that will create new instance with 0%

Local development

To run tests locally you can use provided docker setup. Just run:

docker/build

to build the image and then run:

docker/run composer test

to run the tests. If you want to run tests with coverage, you can run:

docker/run composer test:coverage

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固