View Single Post
ShadowOfGed
Travels via TARDIS
 
Join Date: Aug 2005
Location: Earthsea
 
2009-10-05, 01:41

Wait, nobody's going to suggest the generic solution?
  • Concatenate inputs into a list of numbers
  • Calculate sum of all values
  • Obtain random value (rand * sum)
  • Find greatest i where sum(list[0..i]) < value
  • Return i (your index)

That would work for more than 3! And in some ways, may better represent what's happening. Not saying it's *faster* at all.

Apparently I call the cops when I see people litter.
  quote