SP1 Exercise 1 Index   <<   >>


 

Generate random numbers selected from ⍳≢⍺ according to the weights ,  
a vector of real numbers greater than 0. For example, if

   t←7 5 6 4 7 2 ran 1e6

then in t  , 1 should occur roughly as often as 5 and 3.5 times as often as 6 .



























   ran←{(0,+\⍺÷+/⍺)⍸?⍵⍴0}


   a← 7 5 6 4 7 2
   t←a ran 1e6

   +/ (⍳≢a)∘.=t
226234 161255 193030 129190 225895 64396
   1e6 × a÷+/a
225806 161290 193548 129032 225806 64516.1

The technique can be used to generate random numbers according to a probability distribution.

If a discrete distribution with values v and corresponding probabilities p ,   then v[p ran ⍵]

If a continuous distribution, convert it into a discrete one by dividing (¯∞,∞) into an appropriate number of intervals. The interval mid points are the values; the probabilities are the differences of the cumulative distribution function at the interval end points.