[英]How can I generate 30 random numbers from 1 to 100 in PHP
我想從 1 到 100 生成 30 個隨機數,並避免一個數字被生成兩次。 因此,例如,在 30 個隨機數中,數字 9 不會多次生成。
我怎樣才能做到這一點?
一種有效的方法可能是:
它應該是隨機的和不同的值。 嘗試:
// Generate an array of numbers from 1 to 100
$numbers = range(1,100);
// Random shuffle the array
shuffle($numbers);
// Take first 30 values out of the array (it will be random and distinct)
$random_30 = array_slice($numbers, 0, 30);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.