Thursday, February 7, 2013

Random Name Chooser

I'm teaching a course this semester that introduces graduates to research methods. One of the fundamental concepts is random assignment, that is, randomly choosing people from a pool of participants to some research condition or activity. To have a little fun while also modeling this simple, yet very important idea, I had put everyone's name on a slip of paper, then folded each and placed them in a Pittsburgh Steeler hat -- a literal example of then drawing a name from the hat each week for some "lucky" individual to give the rest of the class his/her interpretation of an assigned reading or podcast.

This week I decided to shift to an electronic means of choosing one of the class members at random. So, I created a small LiveCode project that does just that. I originally intended to just take 2 minutes to create a stack that simply selected a number at random, corresponding to the total number of people in the class. But, I thought I would have a little fun by creating a tool that allowed me to enter all of the names of the individuals into a text field, and then have LiveCode choose and display one of the names at random. This took about 10 minutes.

I took another 20 minutes to add just a little pizzazz to the file (though "pizzazz" is most likely much too strong a word.) I wanted to add a little suspense, so I added code that gave the impression that the software was running through the names -- fast at first, then ever more slowly -- before narrowing it down to the "winner." Kinda like a wheel spinning (though there are no graphics in this file). Then, the chosen name is animated to the center of the screen. You can have as many or as few names as you wish -- the code first counts how many names are in the text field.

All of this code is in the button "Choose":

on mouseUp
   hide me
   put the number of lines in field "names" into varNumber
   put 1 into varDelay
   put "Spinning the wheel." into line 1 of field "comment"
   repeat 25 times
      put the random of varNumber into varWinner
      put line varWinner of field "names" into field "winner"
      wait varDelay ticks
      add 1 to varDelay
      put "." after line 1 of field "comment"
   end repeat
   put "And the winner is..." into line 1 of field "comment"
   //move to center of screen
   hide field "names"
   move field "winner" to 203, 236 in 1 second without waiting
   move field "comment" to 189, 192 in 1 second
   hide field "title"
   show button "Reset"
end mouseUp

varDelay is the timer -- it starts off at one tick (1/60 second), then adds one tick at each loop. So, by the end, it is pausing almost a half second. I'm also fond of the technique of adding a period to the end of a word or phrase to give the impression that the computer is "working," so I added that as well.

(All of the variables are local -- no need for any global variables.)

The button "Reset" does just that -- resets everything for the next choice.

There are many logical "next steps." For example, right now the file keeps all the names "in the hat," but it would be a relatively simple matter to have the name deleted from the text field after it is chosen. It would also be very cool to have a graphic of a wheel spinning with the names, but I confess to having no idea how to pull that one off. (I think I could manage a spinning wheel, but not one that pulls the names from the text field into each wheel wedge.) 

Here it is a link to the file: random_name-Rieber.livecode

(I put in the names of the United States Presidents for this sample file, but you can substitute any names you like. Just be sure to put one name per line.)

No comments:

Post a Comment