From: Bernard Blackham Date: Mon, 2 Aug 2004 10:19:38 +0000 (+0000) Subject: Randomise gray code. X-Git-Tag: IMPORT-FROM-UCCCVS~68 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=fe057d5b30168e9918d17a239c8bfdd5bffcd46d;p=uccvend-vendserver.git Randomise gray code. --- diff --git a/sql-edition/servers/Idler.py b/sql-edition/servers/Idler.py index 4efb56f..555630f 100755 --- a/sql-edition/servers/Idler.py +++ b/sql-edition/servers/Idler.py @@ -1,6 +1,9 @@ #!/usr/bin/env python import string +from random import random + +orderings = None class Idler: def __init__(self, v): @@ -74,8 +77,34 @@ class TrainIdler(Idler): def reset(self): self.idle_state = 0 +class OrderMaker: + def __init__(self, n=8): + self.n = n + self.a = [] + self.u = [] + self.s = [] + for x in range(0,n): + self.u.append(False) + self.go() + + def go(self): + from copy import deepcopy + if len(self.s) == self.n: + self.a.append(deepcopy(self.s)) + else: + for x in range(0,self.n): + if self.u[x]: continue + self.s.append(x) + self.u[x] = True + self.go() + self.u[x] = False + self.s.pop() + + def order(self): + return self.a + class GrayIdler(Idler): - def __init__(self, v, one=None, zero=None): + def __init__(self, v, one=None, zero=None, reorder=0): self.bits = 8 self.size = 1 << self.bits self.i = 0 @@ -83,6 +112,10 @@ class GrayIdler(Idler): self.v = v self.one = one self.zero = zero + self.reorder = reorder + global orderings + if not orderings: + orderings = OrderMaker().order() def next(self): output = self.do_next_state() @@ -91,6 +124,12 @@ class GrayIdler(Idler): output = string.replace(output, "0", self.zero) if self.one: output = string.replace(output, "1", self.one) + if self.reorder: + global orderings + newoutput = "" + for i in range(0,8): + newoutput += output[orderings[self.reorder][i]] + output = newoutput self.v.display(" %8.8s " % (output)) self.i = (self.i + 1) % self.size @@ -138,3 +177,5 @@ class GrayIdler(Idler): def reset(self): self.i = 0 self.grayCode = 0 + if self.reorder: + self.reorder = int(random()*40320) diff --git a/sql-edition/servers/VendServer.py b/sql-edition/servers/VendServer.py index bed5366..992a65d 100755 --- a/sql-edition/servers/VendServer.py +++ b/sql-edition/servers/VendServer.py @@ -219,6 +219,9 @@ def setup_idlers(v): GrayIdler(v,one="*",zero="-"), GrayIdler(v,one="/",zero="\\"), GrayIdler(v,one="X",zero="O"), + GrayIdler(v,one="*",zero="-",reorder=1), + GrayIdler(v,one="/",zero="\\",reorder=1), + GrayIdler(v,one="X",zero="O",reorder=1), ] idler = choose_idler() @@ -275,10 +278,13 @@ def run_forever(rfh, wfh, options, cf): time_to_autologout = time() + 15 if time_to_idle == None and cur_user == '': - time_to_idle = time() + 2 + time_to_idle = time() + 30 choose_idler() if time_to_idle != None and cur_user != '': time_to_idle = None if time_to_idle is not None and time() > time_to_idle: idle_step() + if time() > time_to_idle + 300: + time_to_idle = time() + choose_idler() mk.update_display()