> First, y is converted to a number base x.  
> Then we genereate an n-"digit" random number
> in a range 0.._1+x^n and if it is less than 
> y we keep it and if it >:y we drop it
> and start over.

dice1 implements the description:

dice1=: 9999&$: : (4 : 0)"0
 d=. x #.^:_1 y
 while. 0 1 -: /:y,:z=. ?d do. end.
 x#.z
)

The original "dice" is more complicated probably
due to optimizations.  I am attempting to
understand what the optimizations are.

BTW, is it "obvious" that dice1 generates uniform 
random numbers?



----- Original Message -----
From: Roger Hui <[EMAIL PROTECTED]>
Date: Thursday, July 13, 2006 1:52 pm
Subject: Re: [Jbeta] monadic '?' (and #. slowdown

> Thanks.  But on the face of it the verb does
> not do the same as your description:
> 
> dice=: 9999&$: : (4 : 0)"0
>  y=. x #.^:_1 y-1
>  label_again.
>  l=.y =&{. r=.?1+{.y
>  while. l *. r<&#y do.
>    if. d>f=.y{~_1+#r=.r,d=.?x do. goto_again. end.
>    l=.d=f
>  end.
>  x #. r,?x#~y-&#r
> )
> 
> That is, "dice" does not generate n random digits.
> What it does is it generates random digits one
> by one, and depending on the digit d:
> 
> d<i{y - keep it, and generate n-1-i more digits
>        without further checking
> d=i{y - increment i and generate the next digit
> d>i{y - discard all digits and start from scratch
> 
> Perhaps this is equivalent to your description,
> but the equivalence is not obvious to me.
> 
> As well, if we "tweak" the verb and omit the
> "discard all digits" part but instead just
> discard the current digit d and generate
> another digit d1 and check again, that seems to
> satisfy your description of "uniform finite 
> average number of discards".  But you must have
> a reason for not doing that, otherwise you would
> have (I guess) gotten rid of the distasteful goto.
> 
> 
> 
> ----- Original Message -----
> From: Andrew Nikitin <[EMAIL PROTECTED]>
> Date: Thursday, July 13, 2006 1:24 pm
> Subject: [Jbeta] monadic '?' (and #. slowdown
> 
> > R&S HUI:
> > >So why are the numbers uniform?
> > 
> > First, y is converted to a number base x (n digit vector) 0&{ is 
> > highest"digit", _1&{ is the "lowest".  Then we genereate an n-
> > "digit" random number 
> > in
> > a range 0.._1+x^n and if it is less than y we keep it and if it 
> > >:y we drop 
> > it
> > and start over.  Assuming uniform and independent distribution 
> of 
> > random"digits" we get uniformly distributed result. The way we 
> > generate and check
> > digits promises uniformly finite average number of discarded 
> > digits per 
> > result (I
> > estimate this limit to be either 1 or 2).


----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to