RandomImageFromAListOfPictures

Summary: Posting a random image from a number of pictures
Version:
Prerequisites:
Status:
Maintainer:
Categories: Images, Links
Users: (view? / edit)

Questions answered by this recipe

- I want a different image, from a list, or a directory, of pictures, to show up, each time a page is loaded.
How can I do that?

Random Image From A List Of Pictures

All sections are optional, you can remove those that do not apply to your recipe, and add new ones.

Using the "rand" expression

Quote from MarkupExpressions#rand :

rand

The "rand" expression returns a random integer.
The first argument is the minimum number to be returned and the second argument is the maximum number to be returned.
If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and RAND_MAX.
If you want a random number between 5 and 15 (inclusive), for example, use (rand 5 15).

|| width=50%
||'-code:-' ||'-result:-' 
||[={(rand)}=]  ||{(rand)}  
||[={(rand 1 99)}=] ||{(rand 1 99)}  
code:result:
{(rand)}1972104851
{(rand 1 99)}63

Therefore, you could name your pictures accordingly,
and use the "rand" expression for the numbered part of the name, in the link.

Example

For example, you could name your pictures ...

warning_sign_8.png
warning_sign_9.png
warning_sign_10.png
warning_sign_11.png

(in order to see the randomness, reload or preview the page a couple of times)
(although, a possible cache issue may not be helping the demonstration...)

code...

http://www.pmwiki.org/pmwiki/uploads/Cookbook/warning_sign_{(rand 8 11)}.png http://www.pmwiki.org/pmwiki/uploads/Cookbook/warning_sign_{(rand 8 11)}.png

result...

This way, you could also create random links.

_^_

Question

- Would it be tidier, if the images were named 01.png ... 09.png, 10.png, 11.png, etc ?
- It would. But I don't know how to use "rand" in this case.

_^_


Comments

See also

See discussion at RandomImageFromAListOfPictures-Talk?