All pastes #68444 Raw Edit

Untitled

public text v1 · immutable
#68444 ·published 2006-06-21 17:04 UTC
rendered paste body
#!/usr/bin/python

# Lotto picker by manny juan  (juanm@wellsfargo.com or manny@bdt.com)

from random import randint
import thread
import threading

def PickLotto(evt,thr,lock):

# every needs to know if the solution is found
#                        number of rolls
#                        loop variable
#                        if it's the final run

       global gfound, groll, gj, gfinal_run
       maxm=56; maxj=5; gj = groll = final_run = gfound = 0; lastn = [27,30,36,38,45,13]
#       maxm=8; maxj=5; gj = groll = gfinal_run = gfound = 0; lastn = [2,3,6,7,8,13]

       while gj <= groll and not gfound:
          v=[]; m=maxm; r=range(m+1)
          for k in range(maxj):
              i=randint(1,m)
              n=r[i]
              r[i]=r[m]
              m -= 1
              v.append(n)
          v.sort()
          v.append(randint(1,46))
          if not gfinal_run:
               if v != lastn:
                    lock.acquire()
                    groll += 1
                    lock.release()
                    if gj % 1000 == 0:
                        lock.acquire()
                        print gj, thr
                        lock.release()
               else:
                    gj = -1; gfinal_run = 1; this=1
          lock.acquire()
          gj += 1
          lock.release()
       gfound = 1
       try:
                if this:
                    print v, gj - 1, groll, i, this, "175,711,536 Win, Thread:", thr
                    # this guy found the solution.  he sets event
                    evt.set()
       except: UnboundLocalError

       return

# immediate-mode commands, for drag-and-drop or execfile() execution
if __name__ == '__main__':

# create event object
        evt = threading.Event()

        lock=thread.allocate_lock()
        for thr in range(10):
                thread.start_new_thread(PickLotto,(evt,thr,lock))

# wait till someone found the solution and set event.  then we all quit and go home
        evt.wait()
else:
        print "Module lotto imported."
        print "To run, type: lotto.run()"
        print "To reload after changes to the source, type: reload(lotto)"
# end of lotto.py


======== OUTPUT ============

[root@nqsyslog01 .t]# ./nr
0 8
1002 6
2000 1
[1, 3, 4, 7, 8, 36] 2975 2970 1 1 175,711,536 Win, Thread: 1

prints are gj loop var followed by thread number.  if thread number abruptly
decreases, a thread is working on the final run.  we know we are near the
solution.