When I was making my generative art I needed to generate a lot of random things. The simplest way to do that with PHP is to use the rand()
function.
The rand()
function selects a random integer (whole number) between 0 and getrandmax()
, or 2 values if you supply them (eg rand( 100, 200 )
). This function is really useful, but on its own it has slightly predictable results. In this article I wanted to cover some of the other techniques I use to generate random numbers with PHP.
random()
PHP’s default random number function is rand() but in JavaScript it’s Math.random()
. The difference between the two is that rand
selects random integers, and Math.random
selects random floats between 0 and 1. Both items are useful in different situations so I wanted to replicate the JavaScript random number for my PHP projects.
Shuffle()
To generate certain elements of the Iso City I would store information in an array, shuffle the contents, and then use the first item in the array. This is a nice simple method for picking a random item from an array. You could also use PHP to pick a random number between 0 and the array size and use that as the index, but I like the simplicity of shuffle.
Using arrays and shuffle() I can also do weighted selections. By this I mean that if I want one result to appear more frequently than another then I can add it to the array more times than the other. Then select from the shuffled array in the same way.
I could probably write a function for generating the weighted list for me but I like to keep things simple so use something like this:
If you want to pick a random item from an array then you can also use PHPs array_rand
function. This returns the key of a random item in the array.
I use shuffle when I want to make use of the entire array in a random order, and array_rand when I want to pick a single item.
Weighted Random Numbers
With rand or random there is a linear distribution between the start and end values. To makes things a bit less consistent I use a weighted random number function.
With this function I can generate random numbers that are weighted towards the lower value. The pow value can be tweaked to adjust the weighting. The higher the number, the further it is skewed to the bottom, the lower the number the smoother the curve.
You can see an example of the distribution here.
Random Numbers with X Digits
Sometimes I want to generate a random number of a certain length/ certain number of digits. For example, I might want to generate a random 4 digit number. I can do this by calculating the range using the pow()
function from PHP.
Probability
This is a simple little tip, but sometimes I want to make something happen with a certain frequency – so I use a one line conditional to decide whether to run the code or not.
Conclusion
In this article I’ve listed a selection of the ways that you can use random numbers in PHP, but I’m sure there are many more techniques. It’s been interesting trying these different methods whilst creating the artworks and seeing how the needs and use change based upon where I am using them.
One thing I’ve not used is random seeds, which would allow me to make reproducable artworks by using the same random numbers each time. I think this would be interesting for creating individual things for specific people using things like email addresses as keys.
I have now used these number randomizing techqniques to generate a large collection of printable puzzles. They are available on my website NinjaPuzzles.
Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).
Link to this page
Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.