From: Daniel Carrera Date: 2003-03-29T17:14:58+09:00 Subject: Re: Weighted random selection -- how would you do this? On Sat, Mar 29, 2003 at 05:04:32PM +0900, Phil Tomson wrote: > >event = events.each do |event, weight| > > val -= weight; > > return event if val < 0 > >end > > Hmmm.... Wouldn't this be biased by order? > Would it? This is my thinking, perhaps you can help me see if I'm wrong: Example: Take two events with weights: event1 => 3 event2 => 7 Pick a random number from 0 to 3+7=10 There is a 30% chance that the number will be between 0 and 3 and there is a 70% chance that the number will be between 3 and 10. By selecting an event based on which range the random number lies in I should get the biased probability that I desire. I think I should replace the "val < 0" in my code by "val <= 0". Is this the error you were referring to? Or is there a problem with my reasoning? -- Daniel Carrera Graduate Teaching Assistant. Math Dept. University of Maryland. (301) 405-5137 -- distrain: distrain (di-STRAYN) verb tr., intr. To seize the property in order to force payment for damages, debt, etc.