TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Tips & Tricks (http://www.talkphp.com/tips-tricks/)
-   -   Random Items without a Database (http://www.talkphp.com/tips-tricks/1527-random-items-without-database.html)

WinSrev 11-27-2007 02:57 AM

Random Items without a Database
 
If you ever wanted to show something random without using the database then this is a decent method:
php Code:
<?php
$banner[]=array("image.gif","http://domain.com/")
$banner[]=array("image2.gif","http://www.domain.domain.co.uk/");
$banner[]=array("image3.gif","http://search.mysearch.com");

$rand = rand(0,count($banner)-1)
?>
<p align="center"
<a href=<?=$banner[$rand][1]?>><img src="<?=$banner[$rand][0]?>" alt="" width="468" height="60" border="0"></a>
</p>

More of a little script than a tutorial, thanks :)

Wildhoney 11-27-2007 03:12 AM

Good tip! There is also the array_rand way. However, as I was trying it just now, it seems to love its zeros, so I decided to do a little test as well. This is the array_rand code:

php Code:
$aFilms = array("V For Vendetta", "Requiem For A Dream", "Jacob's Ladder");
echo $aFilms[array_rand($aFilms)];

And this is my test code as it appeared to be quite fond of returning zero:

php Code:
$aCount = array(0, 0, 0);
$aFilms = array("V For Vendetta", "Requiem For A Dream", "Jacob's Ladder");

for($iIndex = 0; $iIndex <= 5000; $iIndex++)
{
    $aCount[array_rand($aFilms)]++;
}

print_r($aCount);

Results were quite surprising as I thought zero would have been much more dominant:

Quote:

Array
(
[0] => 1701
[1] => 1637
[2] => 1663
)

WinSrev 11-27-2007 03:32 AM

That looks pretty cool, did a test (10,000 times in a loop):
rand();
Result 1: Page generated in 1.496 seconds.
Result 2: Page generated in 1.400 seconds.
Result 3: Page generated in 1.486 seconds.

array_rand();
Result 1: Page generated in 0.087 seconds.
Result 2: Page generated in 0.078 seconds.
Result 3: Page generated in 0.076 seconds.

Didn't realise the results would vary so much.

Wildhoney 11-27-2007 03:37 AM

That is rather surprising, actually. I'd have thought rand would have been faster. In fact, I was under the impression that the array_rand function would have been a wrapper for the rand function, with 1 or 2 adaptations in that it recognised the array's upper boundary by calling the count function as well.

iisbum 11-27-2007 02:32 PM

My favorite way of doing this is to use the shuffle function, to use the example from above:

PHP Code:

$banner = array();
$banner[]=array("image.gif","http://domain.com/");  
$banner[]=array("image2.gif","http://www.domain.domain.co.uk/");
$banner[]=array("image3.gif","http://search.mysearch.com");
shuffle($banner);

$numBanners 1;
for (
$i=0$i<$numBanners$i++) {
  print 
"".$banner[$i][0];


This way you won't duplicate ads if you are showing more than one at a time. I haven't tested its efficiency, but since you randomize the array once I'm guessing its pretty efficient.

Mubs

Wildhoney 11-27-2007 03:03 PM

Interesting, Mubs. The only resevation I'd have about using the array_shuffle function is that it alters the arrangement of the items in the array permanantly. IE: you cannot return them to their previous state unless of course one of the *sort* functions does it - for which you would have needed to construct the array into some sort of arrangement to begin with, such as alphabetical.

iisbum 11-27-2007 06:59 PM

Agreed, in my case I didn't need to re-sort just randomize and display so no problems there.

If its a short, simple array, it's easy to just make a copy of the array for randomizing and leave the original sorted as you need.

Mubs

EyeDentify 11-28-2007 11:31 AM

My way of outputing a random logo
 
PHP Code:

function random_logo() {

    
$szLogo[0] = "mb_v6_logga_800x150.jpg";
    
$szLogo[1] = "mb_v6_logga_800x150_2.jpg";
    
$szLogo[2] = "mb_v6_logga_800x150_3.jpg";
    
$szLogo[3] = "mb_v6_logga_800x150_4.jpg";
    
$szLogo[4] = "mb_v6_logga_800x150_5.jpg";
    
$szLogo[5] = "mb_v6_logga_800x150_6.jpg";
    
$szLogo[6] = "mb_v6_logga_800x150_7.jpg";
    
$szLogo[7] = "mb_v6_logga_800x150_8.jpg";
    
$szLogo[8] = "mb_v6_logga_800x150_9.jpg";
    
$szLogo[9] = "mb_v6_logga_800x150_10.jpg";
    
$szLogo[10] = "mb_v6_logga_800x150_11.jpg";
    
$szLogo[11] = "mb_v6_logga_800x150_12.jpg";

$szNumber_of_logos count($szLogo);
$iRand_nr mt_rand(0$szNumber_of_logos);
$szRandomLogo $szLogo[$iRand_nr];

// If no logo is returned use a standard one
if ($szRandomLogo == "") {
    
$szRandomLogo "mb_v6_logga_800x150.jpg";
}
    return 
$szRandomLogo;




This is a little function i wrote quickly for a website i had a few years ago, were i wanted to show a random header with the site logo. Itīs not wery effecient i guess neither does it check that the file is actually there so theres room for improvement.

what it does is just return a filename of a logo to display.

Tried to apply your prefered variable namning convention :D

Hope it give someone ispiration.

Canīt remember why i numbered the array thoug.

d4v1d 12-04-2007 12:55 PM

Excuse the newbie, but isn't it best practice to seed the random function before calling?

Wildhoney 12-04-2007 01:29 PM

You're right in a way, but I believe they now seed automatically before spitting you out a number. As of around PHP version 4.2.0, apparently. Before 4.2.0 though you most certainly would seed the number.

d4v1d 12-04-2007 01:50 PM

Quote:

Originally Posted by php.net
Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.

Just read that off php.net... should've checked first, my bad. :-$

Wildhoney 12-05-2007 02:50 AM

The funny thing is, when I was a PHP beginner, and maybe even an intermediate as well, I used PHP versions earlier than 4.2.0, and I never remember seeding before I output a random value. Oooops!?


All times are GMT. The time now is 09:01 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0