From: "Mauricio Fernández" Date: 2003-03-30T06:17:55+09:00 Subject: [OT] Re: Weighted random selection -- how would you do this? On Sun, Mar 30, 2003 at 05:30:43AM +0900, Austin Ziegler wrote: > On Sun, 30 Mar 2003 04:07:22 +0900, Phil Tomson wrote: > > I tried running it a few times and well it seems to work. > > > > I was just thinking that the order of the elements in the list > > could influence which is picked (for example if elements with high > > weights appeared earlier or later in the list), but it doesn't It doesn't affect iff your uniform RNG is *really* uniform. > > seem to be the case. Certainly a lot more compact and faster than > > my RouletteWheel example. > > Couldn't order affect the results if more than one item share the > same weight? Should work just fine regardless of the order or the weights. The same method can be used to generate random variables with an arbitrary distribution function (Rayleigh, Gaussian, Poisson, Exponential, ...), ie. doing F(u) where F(x) = P( X <= x ) is the desired dist. func. of the generated random variable U is a uniform random variable -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com "You, sir, are nothing but a pathetically lame salesdroid! I fart in your general direction!" -- Randseed on #Linux