The Mega Millions lottery requires picking 5 distinct numbers in the
range 1-56 and one number in the range 1-46.  The verb pick below
randomly chooses a lottery ticket, and uses verb ci from

http://www.jsoftware.com/jwiki/Essays/Combination Index

ci=: 4 : 0 " 1 0
 'm n'=. x: x
 z=. 0$q=. n
 for_p. (-i.)m do.
  k=. (p,q) lead y
  y=. y-(p!q)-p!q-k
  q=. q-1+k
  z=. z,k
 end.
 z + (i.#z) + |.!.'' +/\z
)

lead=: 4 : 0
 'm n'=. x: x
 a=. m!n
 p=. n-1
 q=. m-1
 while. p>:q do.
  j=. q+<.-:p-q
  s=. (a - m!j) - y
  if.     0 > s do. p=. j-1
  elseif. 0 < s do. q=. j+1
  elseif. 1     do. n-j return. end.
 end.
 (n-1)-p
)

pick=:3 : '>:(5 56 ci ?5!56),?46'

   pick"0 i.5
 9 26 30 38 42 30
12 23 29 48 52 24
11 18 21 25 30  9
 1  5 14 19 43 12
15 25 35 42 48  6

I can also do it less elegantly but from first principles as

pick2=:3 : 0
a=.0$0
while. 5>#a do. a=.~. a, ?56 end.
>:(/:~ a),?46
)

   pick2"0 i.5
 2 32 40 45 48  1
 7 28 29 35 54 11
 1 26 31 41 55  3
 3 11 29 41 43 32
12 20 24 27 41  9

Is there a better way to do it?  I can't seem to make pick tacit, and
pick2 seems clumsy.

Best wishes,

John


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

Reply via email to