FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Filling an array with random input doesn't quite work
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Filling an array with random input doesn't quite work [message #185273 is a reply to message #185211] Sun, 16 March 2014 14:45 Go to previous messageGo to previous message
Gabriel is currently offline  Gabriel
Messages: 11
Registered: March 2014
Karma:
Junior Member
On 12/03/2014 17:18, richard wrote:
> $x=1;
> while ($x<40){
> $yr=range(60,69);
> shuffle($yr);
> $ayr[$x]=$yr[$x];
> echo $ayr[$x]."<br>";
> $x++;
> }
>
>
> I am attempting to load an array based upon the range of numbers 60 through
> 69.
> The array will have 40 items.
> This code produces 9 items just fine.
> The rest of the array is empty.
> What am I missing?
>

I would do it like this:

$arr = array_fill(0, 40, 0);
array_walk($arr, function(&$v, $k) { $v = rand(60, 69); });

That will provide you with an array containing 40 elements, each a
random integer between 60 and 69:


print_r($arr);
Array
(
[0] => 60
[1] => 69
[2] => 63
[3] => 63
[4] => 64
[5] => 65
[6] => 68
[7] => 67
[8] => 67
[9] => 67
[10] => 63
[11] => 69
[12] => 65
[13] => 60
[14] => 68
[15] => 62
[16] => 67
[17] => 65
[18] => 60
[19] => 65
[20] => 62
[21] => 69
[22] => 69
[23] => 64
[24] => 68
[25] => 65
[26] => 69
[27] => 66
[28] => 61
[29] => 69
[30] => 61
[31] => 61
[32] => 68
[33] => 64
[34] => 64
[35] => 63
[36] => 60
[37] => 63
[38] => 60
[39] => 67
)
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: readdir lists randomly
Next Topic: Nested PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 20:38:07 GMT 2024

Total time taken to generate the page: 0.04231 seconds