jarrodwhaley.com

I make films. I'm also a nerd.

A Flexible Random Number Generator on iOS

My girlfriend manages a retail store on the Web, which entails a lot of marketing via social media. She likes to run a weekly promotion on Facebook in which a prize is awarded to a randomly selected “liker”1. When she first conceived the idea, she didn’t really have a means of properly choosing a winner with any real amount of randomness2, so I thought I’d cook up a little Python script. I realized that something like this would be a handy little utility for my own purposes, and I also knew my girlfriend would be asking for random numbers with some degree of regularity. Those two conditions being the case, I decided I’d like to write something that could be easily fired from an iPhone or an iPad with minimal effort. The script below meets those requirements fairly well, I think.

You’ll obviously want to run this in Pythonista. The cool part–the thing that differentiates this script from any old “My First Program” random number generator–is the way in which the user’s I/O is handled. It can be triggered either by running it directly in Pythonista, or via a Drafts action:

pythonista://RandGen?action=run&argv=[[title]]

When run from within Pythonista, you’ll be presented with an alert dialog into which you can input the upper limit of the range within which you need to get a random number; the output will be shown to you in a second alert, and will also be sent to the device’s clipboard.

When triggered from Drafts, given an input of the upper limit of your desired numerical range, Pythonista will run the script and then send the output back to Drafts, from which it can then be passed along via any other Drafts action of the user’s choice.

Handy.


  1. I’m punching my own face in punishment of techno-neologistic douchery even as I fail to think of a better word. 
  2. Yes, I know, true randomness is one of computation’s oldest problems. Whatever.