Building a Bespoke Random Number Generator
Posted on Friday 19th October 2012 at 16:22

My girlfriend is a teacher, and often, when she is producing material for her maths lessons, she will ask me to think up a random number for her. This is pretty easy when she needs one or two, but it gets a little more difficult when she decides she needs 20 or 30, especially if they have restrictions on them, such as they must be able to be divided by 10, they must be 4 digits long or they must have 2 decimal places.
So far, so simple.
This is a simple tool with a very limited range of useful functions, but for what it is designed to do, it is far quicker and easier than working out numbers by hand. The tool took a couple of hours to perfect, which means that if I were building it commercially, it'd be a pretty cheap investment for any size of business, which, if used by enough people within an organisation for long enough, could begin to save many valuable man hours.
So in the end, I decided it'd be quicker and easier for me to just make her a random number generator webapp using HTML5 and PHP. The most basic version took me about 20 minutes to build, and in total I've spent about 2 hours on the final version, which you can have a play with here. It's not exactly the world's most complicated project, but I learnt one or two things from it, so I thought I'd write it up and share the knowledge.
Getting Started
I began by creating a simple HTML form with two input fields, one for the number of numbers to be generated, and the other for the number of digits long that each number should be. The form posts to itself, at which point we have two PHP operations to perform. The first one is to transform the number of digits required into a minimum and a maximum for the random number generator, and the second is loop for as many numbers as are required, generate a random number that meets the parameters each time, and then echo the number back to the browser. Together they look like this:
// Set the minimum possible number with the correct number of digits
$zeros = $_POST['digits']-1; // The number of zeros required, e.g. 100 has 2 zeros
$min = pow(10, $zeros); // 10 to the power of $zeros, e.g. 10^2 = 100
// Set the maximum possible number with the correct number of digits
$max = pow(10, $_POST['digits']);
$max--; // Minus 1, e.g Maximum 3 digit number is 999; 10^3 = 1000-1 = 999
// Cycle through as many iterations as are required
for ($i = 1; $i<= $_POST['results']; $i++) {
$number = mt_rand($min, $max); // Random number generator with assigned minimum and maximums
echo $number;
}
So far, so simple.
Adding Decimals
Next we want to be able to add a set number of digits after the point. It's entirely possible that there is a simple way of doing this that PHP Maths experts know (if you know of one, please comment and let me know!), but the best solution I could come up with was to repeat the above process to generate a second random number, and then join the two numbers together as a string with a decimal point between them:
// Generate decimal and join them together
$decimal = mt_rand($dec_min, $dec_max);
$number = $number.".".$decimal;
}
Setting Intervals
Since this tool is designed to produce random numbers for use in maths lessons, the final requirement I needed to build in was the ability to set intervals in the "random" numbers, for example, we might want to generate numbers that only end in 5 or 0, or we might only want numbers in 3 times table. Once again, I wasn't aware of a very good way of doing this, so I had to improvise a little bit.
First, I created an array listing the first 20 numbers that fitted the rule. Then, when the random numbers were being generated, I used a regular expression match to determine if the numbers being generated fitted the required rule, before outputting them.
// Produce an array of number endings that fit the required interval pattern
$interval = $_POST['interval'];
$intervals = array();
for ($i = 1; $i<= 20; $i++) {
array_push($intervals, $interval*$i);
}
// Create a regular expression pattern from the array
$pattern = implode("|", $intervals);
$pattern = "/(".$pattern.")$/"; // Checks if the end of the generated number matches one of the pattern options
// Whilst loop is running, check randomly generated number against the pattern
if(!preg_match($pattern, $number)) {
// If pattern match fails, reduce the cycle count by 1 and then begin the next loop.
$i--;
continue;
}Conclusion
This is a simple tool with a very limited range of useful functions, but for what it is designed to do, it is far quicker and easier than working out numbers by hand. The tool took a couple of hours to perfect, which means that if I were building it commercially, it'd be a pretty cheap investment for any size of business, which, if used by enough people within an organisation for long enough, could begin to save many valuable man hours.
This is just one example of how a webapp can seriously speed up business admin or other routine tasks. There are loads of others, and I hope that in the future, many more businesses will be looking for ways to speed up the work their employees do.
You can try the tool here.














