FM Function of the Week: Random

Aah randomness with its ability to surprise and delight. Not like that miscreant entropy hurtling the universe toward heat death. There are times when you simply need to inject a bit of the unexpected into your world (or at least your database). For this, FileMaker has created the Random function. When invoked, Random will deliver a pseudo-random number between zero and one.

Wait- pseudo random? You didn’t hand over pseudo money for PseudoMaker. Why should you have to put up with second-class pseudo-random numbers? This isn’t FileMaker’s fault. True randomness is at cross-purposes with the deterministic nature of computers. MIT Engineering has a nice short explanation of why. And if you feel you really must have a bona fide random number, try the services at random.org where they have an approach to marry analog randomness to web services. Anyway, distinguishing pseudo-random from true random is beyond the mental capacity of a human brain, so let’s just roll with it.

Example output of the Random functionThere’s an example of a random number from FileMaker. The only guarantee you’ll get is that it’s somewhere between zero and one. FileMaker will generate a number that is 20 digits long, but if one or more of the rightmost digits is a zero, they won’t be displayed. In practice, a 20 digit decimal is rarely what you want anyway. More likely, you’ll want to generate a positive whole number and a little extra math can bring that to bear.

show the calclulation Int ( n * Random ) + 1 = 1...nThis formula shows how to get a whole number in the range of 1 to n. The Int function (which¬†showed up in last week’s post) takes any number and lops off its decimal values leaving you with just the integer. Inside that Int function is “n * Random“. This is where we’re multiplying any given number (represented by n) by a random number between zero and one. The result of that calculation will always return a value in the range of zero to n-1. Tacking a “+1” on the end ensures a positive result. So if you use, say, the number 10 in place of the n, you’ll get a random number between 1 and 10.

If you want to take your database user to a randomly selected record in your database, the following formula in a Go to Record script step will generate the record number for you.

Int ( Get ( FoundCount ) * Random ) + 1Extra Credit

Because you know I can’t resist extra credit. Everything above has been for a ranges between zero and one or between 1 and n. This calculation, making good use of our beloved Let function, lets you generate a random number between any two numbers.

Random number within a range: Let ( [   vMinimum = 25;   vMaximum = 100;   vDifference = vMaximum - vMinimum ];    vMinimum + Int ( vDifference * Random ) + 1 )The fact is that we rarely want a truly random number. Most of the time we want a randomly selected value within a certain range. The Random function alone can’t do that, but given a little know-how, Random can be combined with some other FileMaker functions to get you the results you’re seeking.

This entry was posted in FileMaker 13, Function of the Week. Bookmark the permalink.