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
Switch to threaded view of this topic Create a new topic Submit Reply
Filling an array with random input doesn't quite work [message #185211] Wed, 12 March 2014 17:18 Go to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
$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?
Re: Filling an array with random input doesn't quite work [message #185212 is a reply to message #185211] Wed, 12 March 2014 17:36 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <12lin35i3xsev(dot)abk024ld5l8l(dot)dlg(at)40tude(dot)net>, richard
<noreply(at)example(dot)com> 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?

This:

$ayr[$x]=$yr[$x];

isn't gonna work once $x gets above 9, now is it? Your array $yr only
contains 10 items.

--
"I am enclosing two tickets to the first night of my new play; bring a
friend.... if you have one." - GB Shaw to Churchill "Cannot possibly
attend first night, will attend second... if there is one." - Winston
Churchill, in response.
Re: Filling an array with random input doesn't quite work [message #185213 is a reply to message #185211] Wed, 12 March 2014 19:02 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
richard <noreply(at)example(dot)com> wrote in news:12lin35i3xsev.abk024ld5l8l.dlg@
40tude.net:

> $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?

This line:

$ayr[$x]=$yr[$x];

What is the "value" of $yr[$x] when x is greater than 9?
Re: Filling an array with random input doesn't quite work [message #185214 is a reply to message #185211] Wed, 12 March 2014 19:06 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
richard <noreply(at)example(dot)com> wrote in news:12lin35i3xsev.abk024ld5l8l.dlg@
40tude.net:

> $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?

This may be helpful -- specifically the % operator.

http://www.php.net/manual/en/language.operators.arithmetic.php
Re: Filling an array with random input doesn't quite work [message #185215 is a reply to message #185211] Wed, 12 March 2014 20:34 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Wed, 12 Mar 2014 13:18:19 -0400, 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?

You're missing a richardian fuck up in the array indexing!

You also seem to be ignoring a message something like:

"PHP Notice: Undefined offset: 38 in /richardian_array_fuckup.php on
line 13"

Try:

$x=1;
while ($x<40){
$yr=range(60,69);
shuffle($yr);
$n = count($yr);
echo "\$x is {$x}, size of \$yr is {$n} elements, \$yr[\$x] is '{$yr
[$x]}'<br>\n";
$ayr[$x]=$yr[$x];
echo $ayr[$x]."<br>\n";
$x++;
}

and see if that helps you work it out.

By the way, the following code might be useful:

$a=40;
$b=array();
$c=range(60,69);
while(--$a)$b[$a]=$c[array_rand($c)];

assuming that what you actually want is an array of 39 elements indexed
1..39 with random values from 60 to 69.

I doubt that's exactly what you do want, but that's my best guess at what
your code is intended to create.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) [message #185216 is a reply to message #185211] Wed, 12 March 2014 21:08 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Wed, 12 Mar 2014 13:18:19 -0400, 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?

http://mroldies.net/daily/daily2.php

$x=1;
while ($x<40){
$yr=rand(1,10);
$yr=$yr+59;

$num=rand(1,100);
if ($num<100){$num="0".$num;}
if ($num<10){$num="0".$num;}

$song[$x]="http://mroldies.net/audio/19$yr/".$yr."-".$num.".mp3";

$x++;

}


At this point though, I don't have it worked out to check for duplicats.
Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) [message #185217 is a reply to message #185216] Wed, 12 March 2014 22:49 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <85dkidwa0h67(dot)ecj3ezgrqtzk$(dot)dlg(at)40tude(dot)net>, richard
<noreply(at)example(dot)com> wrote:

You're welcome.

> $x=1;
> while ($x<40){
> $yr=rand(1,10);
> $yr=$yr+59;
>
> $num=rand(1,100);
> if ($num<100){$num="0".$num;}
> if ($num<10){$num="0".$num;}

What is the point if this second "if"? You've already covered that with
the first.

>
> $song[$x]="http://mroldies.net/audio/19$yr/".$yr."-".$num.".mp3";
>
> $x++;
>
> }

--
"The idea that Bill Gates has appeared like a knight in shining armour to
lead all customers out of a mire of technological chaos neatly ignores
the fact that it was he who, by peddling second-rate technology, led them
into it in the first place." - Douglas Adams
Re: Filling an array with random input doesn't quite work [message #185218 is a reply to message #185215] Wed, 12 March 2014 22:51 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article <lfqgdh$gjv$2(at)dont-email(dot)me>, Denis McMahon
<denismfmcmahon(at)gmail(dot)com> wrote:

> assuming that what you actually want is an array of 39 elements indexed
> 1..39 with random values from 60 to 69.

And of course he will, unavoidably, get duplicates.

--
"I am enclosing two tickets to the first night of my new play; bring a
friend.... if you have one." - GB Shaw to Churchill "Cannot possibly
attend first night, will attend second... if there is one." - Winston
Churchill, in response.
Re: solved! Re: Filling an array with random input doesn't quite work [message #185219 is a reply to message #185217] Thu, 13 March 2014 00:11 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
Tim Streater wrote:

> In article <85dkidwa0h67(dot)ecj3ezgrqtzk$(dot)dlg(at)40tude(dot)net>, richard
> <noreply(at)example(dot)com> wrote:
>
> You're welcome.
>
>> $x=1;
>> while ($x<40){
>> $yr=rand(1,10);
>> $yr=$yr+59;
>> $num=rand(1,100);
>> if ($num<100){$num="0".$num;}
>> if ($num<10){$num="0".$num;}
>
> What is the point if this second "if"? You've already covered that with
> the first.

That serves to add padding to three characters. The last three lines
could be replaced with the following:

str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);

>>
>> $song[$x]="http://mroldies.net/audio/19$yr/".$yr."-".$num.".mp3";
>> $x++;
>> }

--
Christoph M. Becker
Re: solved! Re: Filling an array with random input doesn't quite work [message #185220 is a reply to message #185217] Thu, 13 March 2014 01:14 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Wed, 12 Mar 2014 22:49:38 +0000, Tim Streater wrote:

> In article <85dkidwa0h67(dot)ecj3ezgrqtzk$(dot)dlg(at)40tude(dot)net>, richard
> <noreply(at)example(dot)com> wrote:
>
> You're welcome.
>
>> $x=1;
>> while ($x<40){
>> $yr=rand(1,10);
>> $yr=$yr+59;
>>
>> $num=rand(1,100);
>> if ($num<100){$num="0".$num;}
>> if ($num<10){$num="0".$num;}
>
> What is the point if this second "if"? You've already covered that with
> the first.
>

I do it this way because I've been doing the same method in BASIC for
years. It works fine.
It also helps for sorting purposes.
by having all the strings the same length helps to guarantee that sorting
will not screw it up.

When the number equals 1,
the first "if" adds a zero.
The second "if" adds another zero because it is less than 10.
numbers between 10 and 100 only get one zero added.
Re: solved! Re: Filling an array with random input doesn't quite work [message #185221 is a reply to message #185219] Thu, 13 March 2014 02:43 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Thu, 13 Mar 2014 01:11:59 +0100, Christoph Michael Becker wrote:

> Tim Streater wrote:
>
>> In article <85dkidwa0h67(dot)ecj3ezgrqtzk$(dot)dlg(at)40tude(dot)net>, richard
>> <noreply(at)example(dot)com> wrote:
>>
>> You're welcome.
>>
>>> $x=1;
>>> while ($x<40){
>>> $yr=rand(1,10);
>>> $yr=$yr+59;
>>> $num=rand(1,100);
>>> if ($num<100){$num="0".$num;}
>>> if ($num<10){$num="0".$num;}
>>
>> What is the point if this second "if"? You've already covered that with
>> the first.
>
> That serves to add padding to three characters. The last three lines
> could be replaced with the following:
>
> str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);
>
>>>
>>> $song[$x]="http://mroldies.net/audio/19$yr/".$yr."-".$num.".mp3";
>>> $x++;
>>> }

thanks for the clue. I may use it.
Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) [message #185222 is a reply to message #185217] Thu, 13 March 2014 02:50 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
Tim Streater <timstreater(at)greenbee(dot)net> wrote in news:120320142249386192%
timstreater(at)greenbee(dot)net:

> In article <85dkidwa0h67(dot)ecj3ezgrqtzk$(dot)dlg(at)40tude(dot)net>, richard
> <noreply(at)example(dot)com> wrote:
>
> You're welcome.
>
>> $x=1;
>> while ($x<40){
>> $yr=rand(1,10);
>> $yr=$yr+59;
>>
>> $num=rand(1,100);
>> if ($num<100){$num="0".$num;}
>> if ($num<10){$num="0".$num;}
>
> What is the point if this second "if"? You've already covered that with
> the first.

No, he hasn't. The point is to pad $num on the left with zeros to a consistent length of 3
characters. Values less than 100 are padded with a zero; values less than 10 are padded
again -- so anything between 1 and 9 inclusive gets two padding characters, and values
from 10 to 99 get only one.
Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) [message #185223 is a reply to message #185216] Thu, 13 March 2014 02:53 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
richard <noreply(at)example(dot)com> wrote in news:85dkidwa0h67.ecj3ezgrqtzk$.dlg@
40tude.net:

> $yr=rand(1,10);
> $yr=$yr+59;

Why not this instead?

$yr = rand(60, 69);
Re: Filling an array with random input doesn't quite work [message #185225 is a reply to message #185211] Thu, 13 March 2014 03:02 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
On 03/12/2014 01:18 PM, 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?
>

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


--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Re: Filling an array with random input doesn't quite work [message #185226 is a reply to message #185225] Thu, 13 March 2014 03:11 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/12/2014 11:02 PM, Norman Peelman wrote:
> On 03/12/2014 01:18 PM, 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?
>>
>
> for ($items=1; $items<=40; $items++)
> {
> $ayr[$items] = range(60,69);
> shuffle(&$ayr[$items]);
> }
>
>

Why would you shuffle a single item?


--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: Filling an array with random input doesn't quite work [message #185229 is a reply to message #185225] Thu, 13 March 2014 04:31 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
On 03/12/2014 11:02 PM, Norman Peelman wrote:
> On 03/12/2014 01:18 PM, 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?
>>
>
> for ($items=1; $items<=40; $items++)
> {
> $ayr[$items] = range(60,69);
> shuffle(&$ayr[$items]);
> }
>
>

$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: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) [message #185232 is a reply to message #185217] Thu, 13 March 2014 16:23 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Wed, 12 Mar 2014 22:49:38 +0000, Tim Streater wrote:

> Ricjard Teh Stoopid tried to code something, and this is what escaped
to the internet:

>> $num=rand(1,100);
>> if ($num<100){$num="0".$num;}
>> if ($num<10){$num="0".$num;}

> What is the point if this second "if"? You've already covered that with
> the first.

I think what he means to do is:

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

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: Filling an array with random input doesn't quite work [message #185241 is a reply to message #185226] Fri, 14 March 2014 13:31 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
On 03/12/2014 11:11 PM, Jerry Stuckle wrote:
> On 3/12/2014 11:02 PM, Norman Peelman wrote:
>> On 03/12/2014 01:18 PM, 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?
>>>
>>
>> for ($items=1; $items<=40; $items++)
>> {
>> $ayr[$items] = range(60,69);
>> shuffle(&$ayr[$items]);
>> }
>>
>>
>
> Why would you shuffle a single item?
>
>

$ayr[$items] is an Array

php > print_r($ayr);
Array
(
[1] => Array
(
[0] => 60
[1] => 61
[2] => 62
[3] => 63
[4] => 64
[5] => 65
[6] => 66
[7] => 67
[8] => 68
[9] => 69
)

)

.... after Shuffle:

php > shuffle(&$ayr[1]);
php > print_r($ayr[1]);
Array
(
[0] => 67
[1] => 69
[2] => 66
[3] => 61
[4] => 62
[5] => 60
[6] => 64
[7] => 68
[8] => 63
[9] => 65
)



--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Re: Filling an array with random input doesn't quite work [message #185242 is a reply to message #185241] Fri, 14 March 2014 15:00 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/14/2014 9:31 AM, Norman Peelman wrote:
> On 03/12/2014 11:11 PM, Jerry Stuckle wrote:
>> On 3/12/2014 11:02 PM, Norman Peelman wrote:
>>> On 03/12/2014 01:18 PM, 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?
>>>>
>>>
>>> for ($items=1; $items<=40; $items++)
>>> {
>>> $ayr[$items] = range(60,69);
>>> shuffle(&$ayr[$items]);
>>> }
>>>
>>>
>>
>> Why would you shuffle a single item?
>>
>>
>
> $ayr[$items] is an Array
>
> php > print_r($ayr);
> Array
> (
> [1] => Array
> (
> [0] => 60
> [1] => 61
> [2] => 62
> [3] => 63
> [4] => 64
> [5] => 65
> [6] => 66
> [7] => 67
> [8] => 68
> [9] => 69
> )
>
> )
>
> ... after Shuffle:
>
> php > shuffle(&$ayr[1]);
> php > print_r($ayr[1]);
> Array
> (
> [0] => 67
> [1] => 69
> [2] => 66
> [3] => 61
> [4] => 62
> [5] => 60
> [6] => 64
> [7] => 68
> [8] => 63
> [9] => 65
> )
>
>
>

Sorry, I was in a hurry and wasn't paying as much attention.

But then why have 40 different arrays of 10 items each, anyway?

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: Filling an array with random input doesn't quite work [message #185243 is a reply to message #185242] Fri, 14 March 2014 15:47 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
On 03/14/2014 11:00 AM, Jerry Stuckle wrote:
> On 3/14/2014 9:31 AM, Norman Peelman wrote:
>> On 03/12/2014 11:11 PM, Jerry Stuckle wrote:
>>> On 3/12/2014 11:02 PM, Norman Peelman wrote:
>>>> On 03/12/2014 01:18 PM, 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?
>>>> >
>>>>
>>>> for ($items=1; $items<=40; $items++)
>>>> {
>>>> $ayr[$items] = range(60,69);
>>>> shuffle(&$ayr[$items]);
>>>> }
>>>>
>>>>
>>>
>>> Why would you shuffle a single item?
>>>
>>>
>>
>> $ayr[$items] is an Array
>>
>> php > print_r($ayr);
>> Array
>> (
>> [1] => Array
>> (
>> [0] => 60
>> [1] => 61
>> [2] => 62
>> [3] => 63
>> [4] => 64
>> [5] => 65
>> [6] => 66
>> [7] => 67
>> [8] => 68
>> [9] => 69
>> )
>>
>> )
>>
>> ... after Shuffle:
>>
>> php > shuffle(&$ayr[1]);
>> php > print_r($ayr[1]);
>> Array
>> (
>> [0] => 67
>> [1] => 69
>> [2] => 66
>> [3] => 61
>> [4] => 62
>> [5] => 60
>> [6] => 64
>> [7] => 68
>> [8] => 63
>> [9] => 65
>> )
>>
>>
>>
>
> Sorry, I was in a hurry and wasn't paying as much attention.
>
> But then why have 40 different arrays of 10 items each, anyway?
>

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 [message #185244 is a reply to message #185243] Fri, 14 March 2014 16:55 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/14/2014 11:47 AM, Norman Peelman wrote:
> On 03/14/2014 11:00 AM, Jerry Stuckle wrote:
>> On 3/14/2014 9:31 AM, Norman Peelman wrote:
>>> On 03/12/2014 11:11 PM, Jerry Stuckle wrote:
>>>> On 3/12/2014 11:02 PM, Norman Peelman wrote:
>>>> > On 03/12/2014 01:18 PM, 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?
>>>> >>
>>>> >
>>>> > for ($items=1; $items<=40; $items++)
>>>> > {
>>>> > $ayr[$items] = range(60,69);
>>>> > shuffle(&$ayr[$items]);
>>>> > }
>>>> >
>>>> >
>>>>
>>>> Why would you shuffle a single item?
>>>>
>>>>
>>>
>>> $ayr[$items] is an Array
>>>
>>> php > print_r($ayr);
>>> Array
>>> (
>>> [1] => Array
>>> (
>>> [0] => 60
>>> [1] => 61
>>> [2] => 62
>>> [3] => 63
>>> [4] => 64
>>> [5] => 65
>>> [6] => 66
>>> [7] => 67
>>> [8] => 68
>>> [9] => 69
>>> )
>>>
>>> )
>>>
>>> ... after Shuffle:
>>>
>>> php > shuffle(&$ayr[1]);
>>> php > print_r($ayr[1]);
>>> Array
>>> (
>>> [0] => 67
>>> [1] => 69
>>> [2] => 66
>>> [3] => 61
>>> [4] => 62
>>> [5] => 60
>>> [6] => 64
>>> [7] => 68
>>> [8] => 63
>>> [9] => 65
>>> )
>>>
>>>
>>>
>>
>> Sorry, I was in a hurry and wasn't paying as much attention.
>>
>> But then why have 40 different arrays of 10 items each, anyway?
>>
>
> 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.
>
>
>

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


--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
Re: Filling an array with random input doesn't quite work [message #185250 is a reply to message #185211] Fri, 14 March 2014 19:42 Go to previous messageGo to next message
Daniel Pitts is currently offline  Daniel Pitts
Messages: 68
Registered: May 2012
Karma: 0
Member
On 3/12/14 10:18 AM, 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?
>

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 [message #185254 is a reply to message #185250] Fri, 14 March 2014 22:07 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Daniel Pitts wrote:

> On 3/12/14 10:18 AM, 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?
>
> for ($x = 1; $x < 40; ++$x) {
> $ayr[$x] = rand(60, 69);
> }
>
> So much simpler, more efficient, and actually correct.

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
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Re: solved! Re: Filling an array with random input doesn't quite work [message #185257 is a reply to message #185219] Fri, 14 March 2014 22:23 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Christoph Michael Becker wrote:

> Tim Streater wrote:
>> richard <noreply(at)example(dot)com> wrote:
>>> $x=1;
>>> while ($x<40){
>>> $yr=rand(1,10);
>>> $yr=$yr+59;
>>> $num=rand(1,100);
>>> if ($num<100){$num="0".$num;}
>>> if ($num<10){$num="0".$num;}
>>
>> What is the point if this second "if"? You've already covered that with
>> the first.
>
> That serves to add padding to three characters. The last three lines
> could be replaced with the following:
>
> str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);

$num = str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);

as str_pad() does not modify strings (because they are immutable).

Or simply

$num = sprintf('%03d', rand(1, 100));

:)


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.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
Re: solved! Re: Filling an array with random input doesn't quite work [message #185258 is a reply to message #185257] Fri, 14 March 2014 22:58 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
Thomas 'PointedEars' Lahn wrote:

> Christoph Michael Becker wrote:
>
>> Tim Streater wrote:
>>> richard <noreply(at)example(dot)com> wrote:
>>>> $x=1;
>>>> while ($x<40){
>>>> $yr=rand(1,10);
>>>> $yr=$yr+59;
>>>> $num=rand(1,100);
>>>> if ($num<100){$num="0".$num;}
>>>> if ($num<10){$num="0".$num;}
>>>
>>> What is the point if this second "if"? You've already covered that with
>>> the first.
>>
>> That serves to add padding to three characters. The last three lines
>> could be replaced with the following:
>>
>> str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);
>
> $num = str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);
>
> as str_pad() does not modify strings (because they are immutable).

Of course! Thanks for the correction.

> Or simply
>
> $num = sprintf('%03d', rand(1, 100));
>
> :)

Even better. :)

--
Christoph M. Becker
Re: Filling an array with random input doesn't quite work [message #185263 is a reply to message #185244] Sat, 15 March 2014 03:10 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
On 03/14/2014 12:55 PM, Jerry Stuckle wrote:
> On 3/14/2014 11:47 AM, Norman Peelman wrote:
>> On 03/14/2014 11:00 AM, Jerry Stuckle wrote:
>>> On 3/14/2014 9:31 AM, Norman Peelman wrote:
>>>> On 03/12/2014 11:11 PM, Jerry Stuckle wrote:
>>>> > On 3/12/2014 11:02 PM, Norman Peelman wrote:
>>>> >> On 03/12/2014 01:18 PM, 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?
>>>> >>>
>>>> >>
>>>> >> for ($items=1; $items<=40; $items++)
>>>> >> {
>>>> >> $ayr[$items] = range(60,69);
>>>> >> shuffle(&$ayr[$items]);
>>>> >> }
>>>> >>
>>>> >>
>>>> >
>>>> > Why would you shuffle a single item?
>>>> >
>>>> >
>>>>
>>>> $ayr[$items] is an Array
>>>>
>>>> php > print_r($ayr);
>>>> Array
>>>> (
>>>> [1] => Array
>>>> (
>>>> [0] => 60
>>>> [1] => 61
>>>> [2] => 62
>>>> [3] => 63
>>>> [4] => 64
>>>> [5] => 65
>>>> [6] => 66
>>>> [7] => 67
>>>> [8] => 68
>>>> [9] => 69
>>>> )
>>>>
>>>> )
>>>>
>>>> ... after Shuffle:
>>>>
>>>> php > shuffle(&$ayr[1]);
>>>> php > print_r($ayr[1]);
>>>> Array
>>>> (
>>>> [0] => 67
>>>> [1] => 69
>>>> [2] => 66
>>>> [3] => 61
>>>> [4] => 62
>>>> [5] => 60
>>>> [6] => 64
>>>> [7] => 68
>>>> [8] => 63
>>>> [9] => 65
>>>> )
>>>>
>>>>
>>>>
>>>
>>> Sorry, I was in a hurry and wasn't paying as much attention.
>>>
>>> But then why have 40 different arrays of 10 items each, anyway?
>>>
>>
>> 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.
>>
>>
>>
>
> But each item in your list is an array. He wants single values.
>
>

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 [message #185265 is a reply to message #185254] Sat, 15 March 2014 21:32 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:

> Daniel Pitts wrote:
>
>> On 3/12/14 10:18 AM, 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?
>>
>> for ($x = 1; $x < 40; ++$x) {
>> $ayr[$x] = rand(60, 69);
>> }
>>
>> So much simpler, more efficient, and actually correct.
>
> 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 :)

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 [message #185266 is a reply to message #185265] Sat, 15 March 2014 22:14 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>> Daniel Pitts wrote:
>>> for ($x = 1; $x < 40; ++$x) {
>>> $ayr[$x] = rand(60, 69);
>>> }
>>>
>>> So much simpler, more efficient, and actually correct.
>>
>> 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 :)
>
> Yes, but it's a shame about the extra ')'.

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));

> I think
>
> $ayr = array_map(function () { return rand(60, 69); },
> array_fill(1, 40, null));
>
> is a little simpler.

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.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
Re: Filling an array with random input doesn't quite work [message #185267 is a reply to message #185265] Sun, 16 March 2014 02:14 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/15/2014 5:32 PM, Ben Bacarisse wrote:
> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>
>> Daniel Pitts wrote:
>>
>>> On 3/12/14 10:18 AM, 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?
>>>
>>> for ($x = 1; $x < 40; ++$x) {
>>> $ayr[$x] = rand(60, 69);
>>> }
>>>
>>> So much simpler, more efficient, and actually correct.
>>
>> 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 :)
>
> 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.
>

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(at)attglobal(dot)net
==================
Re: Filling an array with random input doesn't quite work [message #185268 is a reply to message #185267] Sun, 16 March 2014 02:51 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:

> On 3/15/2014 5:32 PM, Ben Bacarisse wrote:
>> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>>
>>> Daniel Pitts wrote:
<snip>
>>>> for ($x = 1; $x < 40; ++$x) {
>>>> $ayr[$x] = rand(60, 69);
>>>> }
>>>>
>>>> So much simpler, more efficient, and actually correct.
>>>
>>> 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 :)
>>
>> 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.
>>
>
> Shorter, maybe - but arguably NOT simpler. Hand that code to a new
> PHP programmer and he/she will have no idea.

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

> It's generally better to code for clarity first.

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 [message #185270 is a reply to message #185268] Sun, 16 March 2014 13:46 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/15/2014 10:51 PM, Ben Bacarisse wrote:
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
>
>> On 3/15/2014 5:32 PM, Ben Bacarisse wrote:
>>> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>>>
>>>> Daniel Pitts wrote:
> <snip>
>>>> > for ($x = 1; $x < 40; ++$x) {
>>>> > $ayr[$x] = rand(60, 69);
>>>> > }
>>>> >
>>>> > So much simpler, more efficient, and actually correct.
>>>>
>>>> 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 :)
>>>
>>> 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.
>>>
>>
>> Shorter, maybe - but arguably NOT simpler. Hand that code to a new
>> PHP programmer and he/she will have no idea.
>
> Surely that would depend on their background would it not? Anyway, I
> meant simpler that the version using array_combine and array_map.
>

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

>> It's generally better to code for clarity first.
>
> Yes, I agree, but clarity is not an absolute, and aiming to be clear to
> beginners puts considerable constraints on design and coding.
>

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(at)attglobal(dot)net
==================
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 next message
Gabriel is currently offline  Gabriel
Messages: 11
Registered: March 2014
Karma: 0
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
)
Re: Filling an array with random input doesn't quite work [message #185286 is a reply to message #185273] Sun, 16 March 2014 23:52 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
On 03/16/2014 10:45 AM, Gabriel Dragffy wrote:
> 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
> )

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 [message #185294 is a reply to message #185273] Mon, 17 March 2014 02:16 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sun, 16 Mar 2014 14:45:42 +0000, Gabriel Dragffy wrote:

> 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
> )

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(at)gmail(dot)com
Re: Filling an array with random input doesn't quite work [message #185304 is a reply to message #185294] Mon, 17 March 2014 12:43 Go to previous messageGo to next message
Gabriel is currently offline  Gabriel
Messages: 11
Registered: March 2014
Karma: 0
Junior Member
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
[…]
)

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 [message #185305 is a reply to message #185304] Mon, 17 March 2014 12:52 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 3/17/2014 8:43 AM, Gabe wrote:
> 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
> […]
> )
>
> 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
>

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

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(at)attglobal(dot)net
==================
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 next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
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
Re: Filling an array with random input doesn't quite work [message #185307 is a reply to message #185306] Mon, 17 March 2014 13:47 Go to previous messageGo to next message
Gabriel is currently offline  Gabriel
Messages: 11
Registered: March 2014
Karma: 0
Junior Member
On 2014-03-17 13:32:57 +0000, Thomas 'PointedEars' Lahn said:

> 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

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 [message #185311 is a reply to message #185307] Mon, 17 March 2014 15:07 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Gabe wrote:

> On 2014-03-17 13:32:57 +0000, Thomas 'PointedEars' Lahn said:
>> Gabe wrote:
>> ^^^^
>>> 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

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

> Dear PointedEars

You are confusing this with private e-mail.

> I am so sorry - I sometimes assume that people (on a PHP dev mailing
> list)

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

> would be able to make very basic adjustments to snippets that I
> posted to accommodate for discrepencies.

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 take it you are not one and have not programmed PHP before and are just
> looking to get started.

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

> Allow me to show you how to make such a change in order to produce an
> array index 1 to 40 inclusive… […]

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


Score adjusted

PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: readdir lists randomly
Next Topic: Nested PHP
Goto Forum:
  

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

Current Time: Sat Nov 23 09:39:20 GMT 2024

Total time taken to generate the page: 0.02428 seconds