Summertime air travel is in full swing here in the US and the Transportation Security Agency is out performing extra, “random” screening at various gates. I’ve watched a few of these recently and I’ve been left with the impression that random is, apparently, decided by the screeners and doesn’t appear random at all. Being a problem solver, I started thinking of how I could use the Force.com platform to help the TSA choose random travelers in a more efficient fashion.

Random Numbers

First, we need some randomness. There are a couple of ways to generate random numbers on the Force.com platform. The first is to call either of the static methods getRandomInteger() or getRandomLong() in the Crypto class. The second is to call either the random() or rint() methods of the Math class. Armed with this knowledge, let’s create a REST based web service that agents can use, at the jet bridge, from their mobile device that instructs them which passengers to choose for extra screening.

There’s a number of ways to do this. The first selects passengers by name, from a list, for random screening. The second, and I believe better, is to provide a list of numbers that show the line positions of people to pull without knowing their identity. For example {1,10,19,103} would tell me to pull the first, tenth, nineteenth and hundred and third passenger out for extra screening.  We could combine the two as well.

Getting Started

Figure 1: Passenger Data Model

Figure 1 shows the simple data model that I am using for this example. I’ve also created a package with the objects and code you can install from this link

The Code

With the data model in place, let’s start writing some code. The first thing we’ll need is a method that generates a random number. We could easily write it like this:

A couple things are happening here. First, on line two we are calling Math.random() which will return a number greater than 0.0 and less than 1. I multiply by 1000 because I want a large range  (to cover hundreds of passengers) and when I round, I get an integer with a range from 0 to 999. Since my flights will have different passenger totals and since it doesn’t make sense to select more passengers for screening than are on the flight, I put a limit on the return value.  By moding the random number with the upper limit, I make sure I never return an integer bigger than what I want. For instance, if I have a flight with 139 people on it, I would pass that as the limit so I never got a result that said pull the two hundred and tenth passenger out of line. Since we don’t want to randomly “select” every passenger let’s put another limit on our method. Let’s specify how many passengers we want to select. We’ll change our code a bit as follows

Here’s the test:

Now we have a method that returns n number of random integers between 0 and upperLimit, which we want. The last thing left to do is make this code mobile friendly. We’ll do that by writing the following Apex REST service.

APEX Rest

Of course we’ll need to test our Apex Rest service before we can deploy it outside of a sandbox. Here’s the test class

All that’s left to do is build, or add this functionality, to a mobile app. Here I show testing the REST Service with Workbench. You could also use another client like curl or Postman but you would have to handle the OAuth authentication and URL escaping yourself.

Here I’ve logged into Workbench. Selected Utilities->Rest Explorer from the menu and inserted the following test URL: /services/apexrest/screeninglist/?flightNumber=99&date=7/10/2013&rands=5  (your data may be different)

In part 1 I’ve shown how to generate random numbers and use them as part of a selection process. In part 2, I’ll modify this slightly to show pulling random records from salesforce. This isn’t a perfect solution and there are some additional details that would need to be added to a production implementation. One thing is that I don’t check for duplicate randoms within my list. It’s possible my return value could look like this: {1,3,100,19,19}. I’m sure readers will find others. I’ll leave those modifications as an exercise to the reader. As always, if you think of a way to make this better please comment.