Filling an array with random input doesn't quite work - Page 2

•  Subject
• Author
• Posted on

Re: Filling an array with random input doesn't quite work

On 03/14/2014 11:00 AM, Jerry Stuckle wrote:

I don't even know why there are 40 items when you are pulling from a
selection of 10 - there's going to be duplicates. Maybe that doesn't
matter... I think someone else actually had what Richard was really after.

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

Re: Filling an array with random input doesn't quite work

On 3/14/2014 11:47 AM, Norman Peelman wrote:

But each item in your list is an array.  He wants single values.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

Re: Filling an array with random input doesn't quite work

On 03/14/2014 12:55 PM, Jerry Stuckle wrote:

Now that I checked his page I see what he was after.

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

Re: Filling an array with random input doesn't quite work

On 03/12/2014 11:02 PM, Norman Peelman wrote:

\$yr = range(60,69);
for (\$items=1; \$items<=40; \$items++)
{
\$ayr[\$items] = \$yr;
shuffle(&\$ayr[\$items]);
}

or

\$yr = range(60,69);
for (\$items=1; \$items<=40; \$items++)
{
shuffle(&\$yr);
\$ayr[\$items] = \$yr;
}

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

Re: Filling an array with random input doesn't quite work

On 3/12/14 10:18 AM, richard wrote:

for (\$x = 1; \$x < 40; ++\$x) {
\$ayr[\$x] = rand(60, 69);
}

So much simpler, more efficient, and actually correct.

Re: Filling an array with random input doesn't quite work

Daniel Pitts wrote:

No, this will produce an array of only 39 elements, assuming there was not
already one.  This can be easily fixed, of course.

Still,

\$keys = range(1, 40);
\$ayr = array_combine(\$keys, array_map(function () {
return rand(60, 69);
}, \$keys)));

is much more elegant :)

PointedEars
--
realism:    HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
-- Bjoern Hoehrmann

Re: Filling an array with random input doesn't quite work

Yes, but it's a shame about the extra ')'.  I think

\$ayr = array_map(function () { return rand(60, 69); },
array_fill(1, 40, null));

is a little simpler.

--
Ben.

Re: Filling an array with random input doesn't quite work

Ben Bacarisse wrote:

Ah, yes, no idea how that got in there.  Should have been

\$keys = range(1, 40);
\$ayr = array_combine(\$keys, array_map(function () {
return rand(60, 69);
}, \$keys));

ACK.  Actually, I had experimented with array_fill() but, strangely enough,
I overlooked this possibility.  (I had posted something similar with regard
to letters not long ago.)

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.

Re: Filling an array with random input doesn't quite work

On 3/15/2014 5:32 PM, Ben Bacarisse wrote:

Shorter, maybe - but arguably NOT simpler.  Hand that code to a new PHP
programmer and he/she will have no idea.

It's generally better to code for clarity first.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

Re: Filling an array with random input doesn't quite work

<snip>

Surely that would depend on their background would it not?  Anyway, I
meant simpler that the version using array_combine and array_map.

Yes, I agree, but clarity is not an absolute, and aiming to be clear to
beginners puts considerable constraints on design and coding.

--
Ben.

Re: Filling an array with random input doesn't quite work

On 3/15/2014 10:51 PM, Ben Bacarisse wrote:

No, it does not.  You may not be the only one looking at it.

Not at all. But not being able to be clear in coding shows a lack of
good programming skills.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

Re: Filling an array with random input doesn't quite work

On 12/03/2014 17:18, richard wrote:

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
)

Re: Filling an array with random input doesn't quite work

On 03/16/2014 10:45 AM, Gabriel Dragffy wrote:

Nice!

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

Re: Filling an array with random input doesn't quite work

On Sun, 16 Mar 2014 14:45:42 +0000, Gabriel Dragffy wrote:

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
[33] => http://mroldies.net/audio/1961/61-058.mp3
[32] => http://mroldies.net/audio/1960/60-026.mp3
[31] => http://mroldies.net/audio/1964/64-090.mp3
[30] => http://mroldies.net/audio/1961/61-080.mp3
[29] => http://mroldies.net/audio/1966/66-059.mp3
[28] => http://mroldies.net/audio/1963/63-066.mp3
[27] => http://mroldies.net/audio/1965/65-006.mp3
[26] => http://mroldies.net/audio/1966/66-050.mp3
[25] => http://mroldies.net/audio/1969/69-004.mp3
[24] => http://mroldies.net/audio/1963/63-100.mp3
[23] => http://mroldies.net/audio/1961/61-071.mp3
[22] => http://mroldies.net/audio/1969/69-082.mp3
[21] => http://mroldies.net/audio/1962/62-075.mp3
[20] => http://mroldies.net/audio/1969/69-080.mp3
[19] => http://mroldies.net/audio/1963/63-039.mp3
[18] => http://mroldies.net/audio/1963/63-022.mp3
[17] => http://mroldies.net/audio/1967/67-096.mp3
[16] => http://mroldies.net/audio/1961/61-041.mp3
[15] => http://mroldies.net/audio/1960/60-013.mp3
[14] => http://mroldies.net/audio/1969/69-012.mp3
[13] => http://mroldies.net/audio/1967/67-087.mp3
[12] => http://mroldies.net/audio/1961/61-009.mp3
[11] => http://mroldies.net/audio/1967/67-044.mp3
[10] => http://mroldies.net/audio/1960/60-021.mp3
[9] => http://mroldies.net/audio/1965/65-094.mp3
[8] => http://mroldies.net/audio/1961/61-071.mp3
[7] => http://mroldies.net/audio/1967/67-040.mp3
[6] => http://mroldies.net/audio/1967/67-044.mp3
[5] => http://mroldies.net/audio/1967/67-081.mp3
[4] => http://mroldies.net/audio/1962/62-022.mp3
[3] => http://mroldies.net/audio/1966/66-017.mp3
[2] => http://mroldies.net/audio/1965/65-005.mp3
[1] => http://mroldies.net/audio/1963/63-076.mp3
)

(although I suspect that he really wants either indexes 1 .. 40 or 0 ..
39, but it's hard to tell which he wants from his code.

Hence my solution of:

\$x=40;
\$song=array();
while(--\$x)\$song[\$x]=sprintf("http://mroldies.net/audio/19%d/%d-%03d.mp3 ",
\$y=rand(60,69),\$y,rand(1,100));

--
Denis McMahon, denismfmcmahon@gmail.com

Re: Filling an array with random input doesn't quite work

On 2014-03-17 02:16:24 +0000, Denis McMahon said:

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
[…]
)

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. The
highlights for me are utilising a PHP native function to walk the array
instead of writing your own iterator using some form of while or for
loop, a lambda function for the processing, and the fantastic
formatting abilities of sprintf.

Cheers

Gabe

Re: Filling an array with random input doesn't quite work

On 3/17/2014 8:43 AM, Gabe wrote:

The question here is not whether YOU find it readable - does the NEW GUY

I've seen more bugs in code because people try to get fancy or short
than when they try to be clear.  Good programmers understand this.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

Re: Filling an array with random input doesn't quite work

Gabe wrote:
^^^^

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.

You are missing the point.

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

Re: Filling an array with random input doesn't quite work

On 2014-03-17 13:32:57 +0000, Thomas 'PointedEars' Lahn said:

Dear PointedEars

I am so sorry - I sometimes assume that people (on a PHP dev mailing
list)  would be able to make very basic adjustments to snippets that I
posted to accommodate for discrepencies. I take it you are not one and
have not programmed PHP before and are just looking to get started.

Allow me to show you how to make such a change in order to produce an
array index 1 to 40 inclusive… You will need to change:

\$arr = array_fill(0, 40, 'http://mroldies.net/audio/19%u/%u-%03u.mp3 ');

to

\$arr = array_fill(1, 40, 'http://mroldies.net/audio/19%u/%u-%03u.mp3 ');

There is no problem storing a repeated prefix in the initial array as
the output array will contain it anyway. Since the value is replaced
in-situ memory usage would be approximately the same. Infact there is
only one array that is programmatically manipulated. If the URL is not
required to be stored in the array then please ask me for help again
and I will be glad to show you how to delete the characters comprising
the URL from the snippet.

Cheers

Gabe

Re: Filling an array with random input doesn't quite work

Gabe wrote:

It is impolite to full-quote a posting, particularly to quote the signature
of a posting without referring to it.  Learn to quote.

You are confusing this with private e-mail.

This is not a mailing list, it is a Usenet newsgroup.  But it makes no
significant difference in that regard:

You have posted *twice* now something that fails to address the problem at
hand.  You have been notified of that twice now.  You have posted a
correction that is suboptimal, and you have been notified of that.  And you
of all people dare to patronize others?  Go away.

I have more than 14 years of professional experience in software development
with PHP now.

If you cared to read, *I* know how it is done.