![]() |
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:
More of a little script than a tutorial, thanks :) |
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:
And this is my test code as it appeared to be quite fond of returning zero: php Code:
Results were quite surprising as I thought zero would have been much more dominant: Quote:
|
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. |
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. |
My favorite way of doing this is to use the shuffle function, to use the example from above:
PHP Code:
Mubs |
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. |
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 |
My way of outputing a random logo
PHP Code:
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. |
Excuse the newbie, but isn't it best practice to seed the random function before calling?
|
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.
|
Quote:
|
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