(PHP)选择三个无重复值的随机数

(PHP) Select Three random number without duplicate value

我必须从1到3获得三个随机数,而结果中没有重复值
和:
$ total ='3';
$ rand1 = rand(1,$ total);
$ rand2 = rand(1,$ total);
$ rand3 = rand(1,$ total);
例如:1.2.3或3.2.1或2.1.3
请帮助我,在此先感谢


如何对数组进行改组,如下所示:

1
2
3
$num = range(1,$total);
shuffle($num);
print_r($num);

使用shuffle()函数对数字1到3的数组进行置换。
随机播放将元素按随机顺序放置在数组中。


为什么不尝试:

1
2
3
4
5
$inp = array(1, 2, 3);
shuffle($inp);
$rand1 = $inp[0];
$rand2 = $inp[1];
$rand3 = $inp[2];


1
2
3
4
5
6
7
8
9
10
11
12
$values = array();  
$max_value = 10;
$count = 5; // must be less than $max_value of you'll get an infinite while loop
for ( $i = 0; $i < $count; $i++ ){
    do {
        $value = rand( 1, $max_value );
    }
    while( isset( $values[ $value ] ) /* && count( $values ) < $count */ )
    $values[ $value ] = TRUE;
}

print_r( array_keys( $values ) );