在 php 中的几个间隔之间生成一个随机数
Generating a random number between several intervals in php
我想在 php 的几个间隔之间创建一个随机数。所以我的数据库中有几个间隔,我想随机生成一个属于这些间隔之一的数字。
我已经考虑过选择一个随机区间,然后在该区间内生成一个随机数,但我认为有更简单和优化的方法。
感谢您的帮助!
您可以使用random_int()
function random_int($min, $max) { }
@param int $min
The lowest value to be returned, which must be PHP_INT_MIN or higher.
@param int $max
The highest value to be returned, which must be less than or equal to PHP_INT_MAX.
@return int
Returns a cryptographically secure random integer in the range min to max, inclusive.
random_int($min, $max)
假设您已经有了 $intervals
列表,您可以遍历它。
foreach($intervals as $interval) {
random_int($interval['min'], $interval['max']);
}
$numbers = 范围(1, 20);
随机播放($numbers);
我想在 php 的几个间隔之间创建一个随机数。所以我的数据库中有几个间隔,我想随机生成一个属于这些间隔之一的数字。
我已经考虑过选择一个随机区间,然后在该区间内生成一个随机数,但我认为有更简单和优化的方法。
感谢您的帮助!
您可以使用random_int()
function random_int($min, $max) { }
@param int $min
The lowest value to be returned, which must be PHP_INT_MIN or higher.
@param int $max
The highest value to be returned, which must be less than or equal to PHP_INT_MAX.
@return int
Returns a cryptographically secure random integer in the range min to max, inclusive.
random_int($min, $max)
假设您已经有了 $intervals
列表,您可以遍历它。
foreach($intervals as $interval) {
random_int($interval['min'], $interval['max']);
}
$numbers = 范围(1, 20);
随机播放($numbers);