Skip to navigation

Use R to help with lottery drawing

When you start using R, it can be confusing that if you assign something to an object you do not get visual feedback:

x <- rep(1:3,5)

You have to then type x to see it:

> x
 [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3

You can exploit this fact for doing a quick-and-dirty lottery. Assume you have a file with email addresses, one per line, called "entries.txt". First, read them and assign them to an object:

entries <- readLines("entries.txt")

Next, create an index of numbers of the same length but in random order:

index <- sample(1:length(entries),length(entries))

Note that you don't see it unless you type index. Combine index and emails in a dataframe (also not directly visible):

dat <- data.frame(cbind(index,entries))

Now, go to and draw a number from the index. Type dat to reveal who you drew. This method makes it a bit more "blind" and a bit less likely that you'll cheat.

Comments are closed.