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

Home » Imported messages » comp.lang.php » Filling an array with random input doesn't quite work
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Filling an array with random input doesn't quite work [message #185215 is a reply to message #185211] Wed, 12 March 2014 20:34 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: readdir lists randomly
Next Topic: Nested PHP
Goto Forum:
  

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

Current Time: Wed Nov 27 21:04:29 GMT 2024

Total time taken to generate the page: 0.04165 seconds