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 #185306 is a reply to message #185304] Mon, 17 March 2014 13:32 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
Gabe wrote:
^^^^
Please fix.

> On 2014-03-17 02:16:24 +0000, Denis McMahon said:
>>> 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
>>> )
>>
>> Yes, but what he wants is actually something like:
>>
>> Array
>> (
>> [39] => http://mroldies.net/audio/1966/66-033.mp3
>> [38] => http://mroldies.net/audio/1964/64-012.mp3
>> [37] => http://mroldies.net/audio/1963/63-085.mp3
>> [36] => http://mroldies.net/audio/1960/60-014.mp3
>> [35] => http://mroldies.net/audio/1960/60-011.mp3
>> [34] => http://mroldies.net/audio/1964/64-065.mp3
>
> Thank you for pointing that out, Dennis.
>
> My code can be trivially modified to do this:
>
>
> $arr = array_fill(0, 40, 'http://mroldies.net/audio/19%u/%u-%03u.mp3');
> array_walk($arr, function(&$v, $k) { $yr = rand(60, 69); $v =
> sprintf($v, $yr, $yr, rand(1, 100)); });
>
>
> Will yield the following array which I have cut short to reduce noise:
>
> print_r($arr);
> Array
> (
> [0] => http://mroldies.net/audio/1966/66-007.mp3
> [1] => http://mroldies.net/audio/1967/67-021.mp3
> [2] => http://mroldies.net/audio/1962/62-016.mp3
> […]
> )

Which *still* is _not_ what was asked for, which is (for whatever reason)
keys from _1_ to _40_ inclusive.

It is also less than optimal to store the repeating prefix in the array, but
you might only have gotten the idea that this was wanted from the precursor.

> Each person prefers their own solution and I am no different. I prefer
> mine because I find it readable whilst only requiring two lines, of
> course you could add a few more line breaks in if preferable. […]

You are missing the point.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
[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: Wed Nov 27 21:22:49 GMT 2024

Total time taken to generate the page: 0.03789 seconds