(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
请帮助我,在此先感谢
如何对数组进行改组,如下所示:
使用shuffle()函数对数字1到3的数组进行置换。
随机播放将元素按随机顺序放置在数组中。
为什么不尝试:
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 ) ); |