# Sorting a list on the fly

I was writing a sort of revision sheet the other day, and I'd written the questions in a sensible order, but wanted to shuffle them randomly so that students wouldn't know what's coming next.

This is not the kind of thing Microsoft Word was built for.

I decided to start a list with the first question I'd written, and decide whether to put the next question before or after it. I'd then do something similar with the next question, until they were randomly arranged. I pulled out a coin... and realised it wouldn't work like that.

You see, I wanted the third question I'd written to be equally likely to land up in slot 1, 2 or 3. If I tossed a coin for each decision, it'd be in slot 1 half the time and the other two slots a quarter of the time. This, of course, was unacceptable.

I got hold of a trusty pocket calculator with a Rnd# function on it. Never mind my computer also has one of those; this was important. And I figured out how to do it, after an embarrassingly long spell of thinking. Longer, in fact, than it would have taken me to write them all out by hand on notecards, shuffle them, and type them out again.

What I did was, generate a random number and multiply by the number of slots available for the question. I'd ignore any decimal part, add 1, and put the next question in that slot.

For example, If there were two questions in the list, there would be three slots. If Ran# $\times 3$ came out to be 1.686, I'd put the next question in slot 2 (between the questions). Had it been 2.82, I'd have put it at the end; had it been 0.224, I'd have put it at the start.

For the next question, I'd multiply by 4 instead of 3 (there would now be three questions in the list, and four slots to put them in), and so on.

I'm not sure this is the most efficient way to randomly sort a list in this context, but it worked for me. Anyone care to prove the questions were arranged fairly?

## Colin

Colin is a Weymouth maths tutor, author of several Maths For Dummies books and A-level maths guides. He started Flying Colours Maths in 2008. He lives with an espresso pot and nothing to prove.

### One comment on “Sorting a list on the fly”

This site uses Akismet to reduce spam. Learn how your comment data is processed.