2013-03-03 84 views
0

我試着去創建100個隨機值的數組從1到1000,並通過4如何在php中創建循環隨機數組?

乘以每個到目前爲止我有:

$numbers = array(rand(1, 1000),rand(1, 1000),rand(1, 1000), 
    rand(1, 1000),rand(1, 1000),rand(1, 1000) 

for($x=0; $x<100; $x++) 
    echo $numbers[$x]*4 . "<br/>"; 

如何獲得蘭特(1,1000)重複沒有複製粘貼100次?謝謝!

回答

2
<?php 
$numbers = array(); 

for ($i = 0; $i < 100; $i++) 
    $numbers[] = rand(1, 1000); 

foreach ($numbers as $number) 
    echo ($number * 4) . "<br />"; 
0

您是否嘗試過這樣或我誤解你的問題:

$randArray=array(); 
for($x=0;$x<100,$x++) 
{ 
    $randArray[]=rand(1,1000)*4; 

    //echo $randArray[$x]; 
} 
0
$numbers = array(); 
for ($x = 0; $x < 100; $x++) { 
    $numbers[] = rand(1,1000) * 4 . "<br>"; 
} 

你當然可以循環再次如果你需要有價值觀沒有他們乘以4爲一些原因。

而不不雅循環的另一種可能的解決方案:

array_map(function() { return rand(1,1000); }, range(1,1000)); 
0
<?php 
$numbers = array(); 
for($i = 0; $i < 100; $i ++) 
{ 
    $numbers[] = rand(1, 1000); 
    echo $numbers[$i] * 4 . "<br/>\n"; 
} 
?>