From: "Hal E. Fulton" Date: 2003-03-29T20:21:11+09:00 Subject: Re: Weighted random selection -- how would you do this? ----- Original Message ----- From: "Tom Payne" Newsgroups: comp.lang.ruby To: "ruby-talk ML" Sent: Saturday, March 29, 2003 4:05 AM Subject: Re: Weighted random selection -- how would you do this? > It's not exactly what you want (but there are good reasons for it > being different), but here's the code I've been using for ages: > > module WeightedRand > > def select > p = rand > each do |prob, value| > return value if p < prob > p -= prob > end > nil > end > > end > > a = [[0.5, :heads], [0.5, :tails]].extend(WeightedRand) > 5.times { puts(a.select) } Again, interesting. But I keep thinking about Phil's comment about order. Is there a consequence to the fact that you are iterating through a list in order? Might the results be "clustered" in some way? I'm not much on probability and stats; and I've been up a long time. :) Hal