Randomness with PHP

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.

Related Posts

28 Sep 2021

Creating Generative Art with PHP

These last few weeks I’ve been experimenting with Generative Art, using PHP. You can see the evolution of my latest series on Twitter. Generative Art is creating artworks through programming. Generative art has a few different names, Procedural art and...
30 Jul 2018

Learning ReactJS for Designers

JavaScript is simple, or at least it used to be. If you’ve done any programming, and you know how to use Google then you can program in JavaScript. Of course, to make it more powerful we have to complicate things.We...
02 Sep 2023

Keeping Web Dev Simple

As I have gotten older, I have realized that caring less about what people think has brought me a great sense of freedom. In the past, I used to follow all the latest trends in tech, constantly trying to keep...
08 Jul 2013

A Redesign for 2013

It’s only been a year since I last redesigned Binary Moon – I’m generally content to redesign, and then sit on things for a while, let it stew and evolve. However a lot has changed in the last year. Design...
13 Dec 2008

A quick way to speed up your website

Getting a faster internet is great – it gives us loads of opportunity to improve the browsing experience through more advanced javascript and designs – I still remember the dreadful websites of the late 90’s that took an hour to...
19 Dec 2023

My Creative Process

In writing about creativity and AI recently, I started reflecting on my own creative process. While I love making things, I’ve always questioned whether or not I’m truly creative. However, I’ve come to realize that my creativity stems from gathering...