Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13103 > unrolled thread

Filling an array with random input doesn't quite work

Started byrichard <noreply@example.com>
First post2014-03-12 13:18 -0400
Last post2014-03-17 16:07 +0100
Articles 20 on this page of 39 — 12 participants

Back to article view | Back to comp.lang.php


Contents

  Filling an array with random input doesn't quite work richard <noreply@example.com> - 2014-03-12 13:18 -0400
    Re: Filling an array with random input doesn't quite work Tim Streater <timstreater@greenbee.net> - 2014-03-12 17:36 +0000
    Re: Filling an array with random input doesn't quite work Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-03-12 19:02 +0000
    Re: Filling an array with random input doesn't quite work Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-03-12 19:06 +0000
    Re: Filling an array with random input doesn't quite work Denis McMahon <denismfmcmahon@gmail.com> - 2014-03-12 20:34 +0000
      Re: Filling an array with random input doesn't quite work Tim Streater <timstreater@greenbee.net> - 2014-03-12 22:51 +0000
    solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) richard <noreply@example.com> - 2014-03-12 17:08 -0400
      Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) Tim Streater <timstreater@greenbee.net> - 2014-03-12 22:49 +0000
        Re: solved! Re: Filling an array with random input doesn't quite work Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-13 01:11 +0100
          Re: solved! Re: Filling an array with random input doesn't quite work richard <noreply@example.com> - 2014-03-12 22:43 -0400
          Re: solved! Re: Filling an array with random input doesn't quite work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-14 23:23 +0100
            Re: solved! Re: Filling an array with random input doesn't quite work Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-14 23:58 +0100
        Re: solved! Re: Filling an array with random input doesn't quite work richard <noreply@example.com> - 2014-03-12 21:14 -0400
        Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-03-13 02:50 +0000
        Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) Denis McMahon <denismfmcmahon@gmail.com> - 2014-03-13 16:23 +0000
      Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-03-13 02:53 +0000
    Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-12 23:02 -0400
      Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-12 23:11 -0400
        Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-14 09:31 -0400
          Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-14 11:00 -0400
            Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-14 11:47 -0400
              Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-14 12:55 -0400
                Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-14 23:10 -0400
      Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-13 00:31 -0400
    Re: Filling an array with random input doesn't quite work Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2014-03-14 12:42 -0700
      Re: Filling an array with random input doesn't quite work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-14 23:07 +0100
        Re: Filling an array with random input doesn't quite work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-15 21:32 +0000
          Re: Filling an array with random input doesn't quite work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-15 23:14 +0100
          Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-15 22:14 -0400
            Re: Filling an array with random input doesn't quite work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-16 02:51 +0000
              Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-16 09:46 -0400
    Re: Filling an array with random input doesn't quite work Gabriel Dragffy <usenet@moojuice.net> - 2014-03-16 14:45 +0000
      Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-16 19:52 -0400
      Re: Filling an array with random input doesn't quite work Denis McMahon <denismfmcmahon@gmail.com> - 2014-03-17 02:16 +0000
        Re: Filling an array with random input doesn't quite work Gabe <usenet@moojuice.net> - 2014-03-17 12:43 +0000
          Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-17 08:52 -0400
          Re: Filling an array with random input doesn't quite work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-17 14:32 +0100
            Re: Filling an array with random input doesn't quite work Gabe <usenet@moojuice.net> - 2014-03-17 13:47 +0000
              Re: Filling an array with random input doesn't quite work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-17 16:07 +0100

Page 1 of 2  [1] 2  Next page →


#13103 — Filling an array with random input doesn't quite work

Fromrichard <noreply@example.com>
Date2014-03-12 13:18 -0400
SubjectFilling an array with random input doesn't quite work
Message-ID<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?

[toc] | [next] | [standalone]


#13104

FromTim Streater <timstreater@greenbee.net>
Date2014-03-12 17:36 +0000
Message-ID<120320141736580579%timstreater@greenbee.net>
In reply to#13103
In article <12lin35i3xsev.abk024ld5l8l.dlg@40tude.net>, richard
<noreply@example.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.

[toc] | [prev] | [next] | [standalone]


#13105

FromDoug Miller <doug_at_milmac_dot_com@example.com>
Date2014-03-12 19:02 +0000
Message-ID<XnsA2EE98DC5D8BBdougmilmaccom@78.46.70.116>
In reply to#13103
richard <noreply@example.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? 

[toc] | [prev] | [next] | [standalone]


#13106

FromDoug Miller <doug_at_milmac_dot_com@example.com>
Date2014-03-12 19:06 +0000
Message-ID<XnsA2EE999DAF924dougmilmaccom@78.46.70.116>
In reply to#13103
richard <noreply@example.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

[toc] | [prev] | [next] | [standalone]


#13107

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-03-12 20:34 +0000
Message-ID<lfqgdh$gjv$2@dont-email.me>
In reply to#13103
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@gmail.com

[toc] | [prev] | [next] | [standalone]


#13110

FromTim Streater <timstreater@greenbee.net>
Date2014-03-12 22:51 +0000
Message-ID<120320142251182204%timstreater@greenbee.net>
In reply to#13107
In article <lfqgdh$gjv$2@dont-email.me>, Denis McMahon
<denismfmcmahon@gmail.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.

[toc] | [prev] | [next] | [standalone]


#13108 — solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)

Fromrichard <noreply@example.com>
Date2014-03-12 17:08 -0400
Subjectsolved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)
Message-ID<85dkidwa0h67.ecj3ezgrqtzk$.dlg@40tude.net>
In reply to#13103
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.
	  

[toc] | [prev] | [next] | [standalone]


#13109 — Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)

FromTim Streater <timstreater@greenbee.net>
Date2014-03-12 22:49 +0000
SubjectRe: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)
Message-ID<120320142249386192%timstreater@greenbee.net>
In reply to#13108
In article <85dkidwa0h67.ecj3ezgrqtzk$.dlg@40tude.net>, richard
<noreply@example.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

[toc] | [prev] | [next] | [standalone]


#13111 — Re: solved! Re: Filling an array with random input doesn't quite work

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-03-13 01:11 +0100
SubjectRe: solved! Re: Filling an array with random input doesn't quite work
Message-ID<5320f7ca$0$6671$9b4e6d93@newsspool2.arcor-online.net>
In reply to#13109
Tim Streater wrote:

> In article <85dkidwa0h67.ecj3ezgrqtzk$.dlg@40tude.net>, richard
> <noreply@example.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

[toc] | [prev] | [next] | [standalone]


#13113 — Re: solved! Re: Filling an array with random input doesn't quite work

Fromrichard <noreply@example.com>
Date2014-03-12 22:43 -0400
SubjectRe: solved! Re: Filling an array with random input doesn't quite work
Message-ID<6wyceu7c8ws5.138qozfqoba4j.dlg@40tude.net>
In reply to#13111
On Thu, 13 Mar 2014 01:11:59 +0100, Christoph Michael Becker wrote:

> Tim Streater wrote:
> 
>> In article <85dkidwa0h67.ecj3ezgrqtzk$.dlg@40tude.net>, richard
>> <noreply@example.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.

[toc] | [prev] | [next] | [standalone]


#13148 — Re: solved! Re: Filling an array with random input doesn't quite work

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-03-14 23:23 +0100
SubjectRe: solved! Re: Filling an array with random input doesn't quite work
Message-ID<4014262.VKcLhA6E0C@PointedEars.de>
In reply to#13111
Christoph Michael Becker wrote:

> Tim Streater wrote:
>> richard <noreply@example.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@news.demon.co.uk>

[toc] | [prev] | [next] | [standalone]


#13149 — Re: solved! Re: Filling an array with random input doesn't quite work

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-03-14 23:58 +0100
SubjectRe: solved! Re: Filling an array with random input doesn't quite work
Message-ID<53238988$0$9510$9b4e6d93@newsspool1.arcor-online.net>
In reply to#13148
Thomas 'PointedEars' Lahn wrote:

> Christoph Michael Becker wrote:
> 
>> Tim Streater wrote:
>>> richard <noreply@example.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

[toc] | [prev] | [next] | [standalone]


#13112 — Re: solved! Re: Filling an array with random input doesn't quite work

Fromrichard <noreply@example.com>
Date2014-03-12 21:14 -0400
SubjectRe: solved! Re: Filling an array with random input doesn't quite work
Message-ID<1drryo1pdixt4$.krnyx0bpn60n.dlg@40tude.net>
In reply to#13109
On Wed, 12 Mar 2014 22:49:38 +0000, Tim Streater wrote:

> In article <85dkidwa0h67.ecj3ezgrqtzk$.dlg@40tude.net>, richard
> <noreply@example.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.

[toc] | [prev] | [next] | [standalone]


#13114 — Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)

FromDoug Miller <doug_at_milmac_dot_com@example.com>
Date2014-03-13 02:50 +0000
SubjectRe: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)
Message-ID<XnsA2EEE8525234Fdougmilmaccom@78.46.70.116>
In reply to#13109
Tim Streater <timstreater@greenbee.net> wrote in news:120320142249386192%
timstreater@greenbee.net:

> In article <85dkidwa0h67.ecj3ezgrqtzk$.dlg@40tude.net>, richard
> <noreply@example.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.

[toc] | [prev] | [next] | [standalone]


#13124 — Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-03-13 16:23 +0000
SubjectRe: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)
Message-ID<lfsm1n$3ba$1@dont-email.me>
In reply to#13109
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@gmail.com

[toc] | [prev] | [next] | [standalone]


#13115 — Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)

FromDoug Miller <doug_at_milmac_dot_com@example.com>
Date2014-03-13 02:53 +0000
SubjectRe: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)
Message-ID<XnsA2EEE8CD887BBdougmilmaccom@78.46.70.116>
In reply to#13108
richard <noreply@example.com> wrote in news:85dkidwa0h67.ecj3ezgrqtzk$.dlg@
40tude.net:

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

Why not this instead?

    	$yr = rand(60, 69);

[toc] | [prev] | [next] | [standalone]


#13117

FromNorman Peelman <npeelman@cfl.rr.com>
Date2014-03-12 23:02 -0400
Message-ID<lfr73e$172$1@dont-email.me>
In reply to#13103
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?-

[toc] | [prev] | [next] | [standalone]


#13118

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-12 23:11 -0400
Message-ID<lfr7lf$3jj$1@dont-email.me>
In reply to#13117
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#13133

FromNorman Peelman <npeelman@cfl.rr.com>
Date2014-03-14 09:31 -0400
Message-ID<lfv0ca$1gb$1@dont-email.me>
In reply to#13118
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?-

[toc] | [prev] | [next] | [standalone]


#13134

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-14 11:00 -0400
Message-ID<lfv5hr$gpd$1@dont-email.me>
In reply to#13133
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.php


csiph-web