www.webdeveloper.com
Results 1 to 5 of 5

Thread: Random element from array

  1. #1
    Join Date
    Mar 2009
    Posts
    36

    Random element from array

    Hi Folks,

    Ive currently got the below code which builds an array.
    I run this with the following commands: build_playlist_array('A'); build_playlist_array('B'); build_playlist_array('C');

    Code:
    function build_playlist_array($list)
    {
    	$playlist_query = "SELECT * FROM t_playlist WHERE list = '" . $list . "'"; 
    	$playlist_result = mysql_query($playlist_query) or die(mysql_error());
    	while($row = mysql_fetch_array($playlist_result))
    	{
    		$items[] = $row['songid'];  
    	}
    }
    Id now somehow like to get the a random item from each array! Any ideas how i can accomplish this? (I will then be running the command using if's and loops to return 3 random items which are obviously not the same as each other)

    Thanks!

  2. #2
    Join Date
    May 2003
    Posts
    599
    This is an easy way, but you may have to tweak it for your app.

    PHP Code:
    $max sizeof($items) - 1;
    $random rand(0,$max);
    echo 
    $items[$random]; 
    edit: this solves the problem of how to select songs randomly, but you may also want to store each random selection to a variable and check it each time through the loop to prevent php's rand() function from selecting the same random number twice in a row and playing the same song twice in a row...If your items array is large, back to back selection would be much more rare than if the items array was small...but it still would be cheesy to play the same song twice.
    Last edited by hastx; 10-24-2009 at 11:42 AM.
    We do precision guesswork

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    PHP Code:
    echo $items[array_rand[$items]] 
    This method does not depend upon the array being sequentially enumerated (e.g. it will work with associative arrays or enumerated arrays with gaps in the indexes).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Nov 2008
    Posts
    2,477
    Quote Originally Posted by NogDog View Post
    PHP Code:
    echo $items[array_rand[$items]] 
    This method does not depend upon the array being sequentially enumerated (e.g. it will work with associative arrays or enumerated arrays with gaps in the indexes).
    Slight typo in the above, should be:

    PHP Code:
    echo $items[array_rand($items)] 

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Quote Originally Posted by Mindzai View Post
    Slight typo in the above, should be:

    PHP Code:
    echo $items[array_rand($items)] 
    Oops...thanks.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles