Welcome Guest (Log in)

Random pick

StarStarStarStarStar
General
Expander triangle
  • Author
  • Rating
  • Type
  • Revision
  • Downloaded
  • Updated
  • Jim Hurley
  • StarStarStarStarStar
  • Stack
  • 1
  • 909 Times
  • 03 November 2010
Pick a random sample from a list
I recently had to pick 200 voters randomly from an election database for a mailer. It occurred to me that the standard method of deleting each line after it is picked (so that it is not picked a second time) is a time consuming operation and using arrays might be much faster. This stack tests the two methods.
1) Faster: Put data into an array. Pick random element from the array. Put the last line in the array into the element just picked, and then omit the last element in the array in the next pick. Repeat. (Time = 5 millisec)
2) Slower: Pick a random line from the data and then delete that line from those available for the next pick. Repeat. (Time = 15 millisec)

Message learned: It takes more time to delete a line from a list--not surprisingly since every line beyond the one deleted has to be reordered--than to just replace on element of an array with another.
Ajax Loader
Tags
Expander triangle
User Comments
Expander triangle