-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
random.php
40 lines (33 loc) · 910 Bytes
/
random.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
declare(strict_types=1);
namespace Psl\Iter;
use Psl\PseudoRandom;
use Psl\Vec;
/**
* Retrieve a random value from a non-empty iterable.
*
* @template T
*
* @param iterable<T> $iterable
*
* @throws Exception\InvalidArgumentException If $iterable is empty.
*
* @return T
*/
function random(iterable $iterable)
{
// We convert the iterable to an array before checking if it is empty,
// this helps us avoids an issue when the iterable is a generator where
// would exhaust it when calling `count`
$values = Vec\values($iterable);
if ([] === $values) {
throw new Exception\InvalidArgumentException('Expected a non-empty iterable.');
}
$size = namespace\count($values);
if (1 === $size) {
/** @var T */
return $values[0];
}
/** @psalm-suppress MissingThrowsDocblock */
return $values[PseudoRandom\int(0, $size - 1)];
}