php 2000 1000到9999之间的唯一随机数

use*_*408 3 php random numbers

我想在1000到9999之间生成2000个唯一的随机数.我正在做这样的事情,但它没有产生唯一的数字.

$numbers = array();             

// Loop while there aren't enough numbers
while (count($numbers) < 2000) {

    $random_number = rand(1000, 9999);

    if (!in_array($random_number, $numbers)) {             
       // This adds the number to the array      
       $numbers[] = 'A'.$random_number.'14';
    }

}
Run Code Online (Sandbox Code Playgroud)

请帮忙.

dec*_*eze 12

而不是运行许多随机尝试,对于像这样的小范围,我宁愿使用这种方法:

$candidates = range(1000, 9999);
shuffle($candidates);
$numbers = array_slice($candidates, 0, 2000);
$numbers = array_map(function ($number) { return "A{$number}14"; }, $numbers);
Run Code Online (Sandbox Code Playgroud)