The current random variate generating verb for
discrete probability functions is shown below. In light of
the new dyadic primitive I. could discreterand be improved?
One issue that might be considered is that typically such
verbs can be sped up by arranging the lookup into the
cumulative probabilities so that the probabilities are
arranged in descending order, not the discrete values. That
way the higher probabilities are encountered earlier and
have a greater chance of quicker lookup.

        I imagine it would be nice if Roger Hui could weigh
in on whether it is likely that I. could be quicker than the
grade approach used in the current discreterand while
allowing for reordering the choices.

rand01=: [EMAIL PROTECTED] 0:

NB. random numbers in a discrete distribution
NB.
NB. y has two elements
NB.   0 =  2-row matrix: 0 =  discrete values
NB.                      1 =  corresponding probabilities
NB.   1 =  number of trials
discreterand=: 3 : 0
'm n'=. y
'v p'=. m
f=. 0,+/\p%+/p
l=. #f
r=. /:f,rand01 n
s=. +/\l>r
r=. s r } r
v {~ <:l}.r
)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to