Re: random numbers from user-defined distributions

*To*: mathgroup at yoda.physics.unc.edu*Subject*: Re: random numbers from user-defined distributions*From*: withoff*Date*: Thu, 7 Jan 93 08:36:47 CST

> does anyone know how to use Random with an arbitrary distribution > function? i would like to generate random numbers in the range {0, Pi} > with a probability distribution function (PDF) of Sin[x]. > > bob > -- > Dr. Robert B. Nachbar | Merck Research Laboratories | 908/594-7795 > nachbar at msdrl.com | R50S-100 | 908/594-4224 FAX > | PO Box 2000 | > | Rahway, NJ 07065 | One method is to map uniform random numbers through the inverse of the corresponding cumulative density function. Unless I've missed something (which is likely) here is how that works out in the present example: In[1]:= cdf[x_] = Integrate[Sin[t], {t, 0, x}]/Integrate[Sin[t], {t, 0, Pi}] 1 - Cos[x] Out[1]= ---------- 2 In[2]:= Solve[t == cdf[x], x] Solve::ifun: Warning: Inverse functions are being used by Solve, so some solutions may not be found. Out[2]= {{x -> ArcCos[1 - 2 t]}} In[3]:= icdf[t_] = x /. %[[1]] Out[3]= ArcCos[1 - 2 t] In[5]:= Table[icdf[Random[]], {10}] Out[5]= {0.74303, 0.725307, 0.507995, 2.57621, 2.18986, 2.43927, 2.20496, > 1.90071, 0.753821, 2.42393} Dave Withoff Wolfram Research