PHP 8.2 新特性之 New Random Extension (新的随机数生成器扩展)

作者: 温新

分类: 【PHP基础】

阅读: 1118

时间: 2023-02-21 06:40:19

hi,我是温新,一名 PHPer

PHP 8.2 引入新的随机扩展, 随机 "扩展为随机数生成提供了一个新的面向对象的API。新的 API 将基于单个 Randomizer 类,该类提供各种随机化方法(如获取 int/字节、随机字符串/数组)。

<span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important"><?</span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">php</span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box"></span></span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$random</span> <span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">=</span> <span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">new</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">Random\Randomizer</span>(<span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">new</span> <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">Random\Engine\Secure</span>());</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(198, 120, 221) !important">echo</span> <span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$random</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-></span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">getInt</span>(<span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>, <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">100</span>) . <span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">PHP_EOL</span>;</span><br></br><span style="box-sizing: border-box;color: rgb(171, 178, 191);padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">print_r</span>(<span style="box-sizing: border-box;color: rgb(224, 108, 117) !important">$random</span><span style="box-sizing: border-box;color: rgb(86, 182, 194) !important">-></span><span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">shuffleArray</span>(<span style="box-sizing: border-box;color: rgb(232, 191, 106) !important">range</span>(<span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">1</span>,<span style="box-sizing: border-box;color: rgb(209, 154, 102) !important">10</span>)));</span>

更多信息参考官方手册:随机数扩展

请登录后再评论