The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2013, 05:10:06 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: array_rand  (Read 1048 times)
Bompa
Administrator
Lifer
*****
Online Online

Posts: 566


View Profile
« on: June 11, 2012, 05:08:50 AM »

I thought it was just me, but this code straight from http://php.net/manual/en/function.array-rand.php
toggles between: 'Morpheus Cypher' and 'Neo Morpheus'.  'Tank' and 'Trinity' never show up.

Code:
<?php
$input 
= array("Neo""Morpheus""Trinity""Cypher""Tank");
$rand_keys array_rand($input2);
echo 
$input[$rand_keys[0]] . "\n";
echo 
$input[$rand_keys[1]] . "\n";
?>


I need to randomly select one item from an array.

/sigh/




Logged

Whenever I point my finger, I have three pointing back at me.
Bompa
Administrator
Lifer
*****
Online Online

Posts: 566


View Profile
« Reply #1 on: June 11, 2012, 05:55:33 AM »

hmmm, I am having the same toggle problem even when I use:

$randNum = rand (1, 1000);
echo "$randNum";

Must be something wrong with my php install.


Logged

Whenever I point my finger, I have three pointing back at me.
ehlo
Journeyman
***
Offline Offline

Posts: 50


View Profile
« Reply #2 on: June 11, 2012, 06:09:50 AM »

Strange, if it helps I usually use shuffle:

Code:
<?php
shuffle
($some_array);
echo 
$some_array[0];
?>


Give that a try.
« Last Edit: June 11, 2012, 06:12:09 AM by ehlo » Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #3 on: June 11, 2012, 08:15:04 AM »

Are you lying this from a browser or the command line? is it possible your browser is caching?
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Bompa
Administrator
Lifer
*****
Online Online

Posts: 566


View Profile
« Reply #4 on: June 11, 2012, 04:35:13 PM »

thanks ehlo.


Using FF with cache disabled with WebDeveloper.

WampServer 2.1 on home pc.

php 5.3.5

Logged

Whenever I point my finger, I have three pointing back at me.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10009



View Profile
« Reply #5 on: June 12, 2012, 09:20:14 AM »

If all you want is a single element from an array, try this. If you still get goofiness then there might be something wrong with your random seed. All that said, I still wonder if there's something going on with your browser and the server. Do you know how to try this right from the command line? I would, just to prove thatPHP is behaving correctly/incorrectly.

Code:
<?php
$input 
= array("Neo""Morpheus""Trinity""Cypher""Tank");
$key rand(0count($input) - 1);
echo 
$input[$key];
?>

Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Bompa
Administrator
Lifer
*****
Online Online

Posts: 566


View Profile
« Reply #6 on: June 12, 2012, 04:21:42 PM »

Do you know how to try this right from the command line?...

I do now.

You were right, it seems to run fine from the command line.


Bompa
« Last Edit: June 12, 2012, 04:32:47 PM by Bompa » Logged

Whenever I point my finger, I have three pointing back at me.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!