Python Forum
Park-Miller Algorithm
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Park-Miller Algorithm
#11
Oh I have a bunch of "add" method.
class Random:
    def __init__(self,seed):
        self.n = seed
    def next(self,range):
        n = ((pow(7,5)*self.n)%(pow(2,31)-1))
        m = (n)%(int(range))
        return m
    def choose(self,letters):
        s = letters
        charak = s[self.next(len(s))]
        return charak
class Mnemonic:
    def __init__(self,seed):
        self.random = Random(seed)
        self.follow = {'a':'','b':'','c':'','d':'','e':'','f':'','g':'','h':'','i':'','j':'','k':'','l':'','m':'','n':'','o':'','p':'','q':'','r':'','s':'','t':'','u':'','v':'','w':'','x':'','y':''}
        self.letters = {'0':'z','1':'q','2':'abc','3':'def','4':'ghi','5':'jkl','6':'mno','7':'prs','8':'tuv','9':'wxy'}
    def add(self,word):
        w = word
        for i in range(0,len(w)-2):
            self.follow[w[i]]+=w[i+1]

    def make(self,number):
        num = number
        s1=''
        s3=''
        print("First number is", num[0])
        s1+=self.random.choose(self.letters[num[0]])
        print("String s1 is",s1)
        for i in num:
            s2 = ''
            s2 += self.follow[i]
            print("String s2 is", s2)
            print("Length of s2 is", len(s2))
            for j in s2:
                if j in self.letters[i]:
                    print("String next num is",self.letters[i])
                    s3+=j
            print("String s3 is now", s3)
        s1 += self.random.choose(self.letters[s3])
        print("String s1 is now",s1)
        return s1

m = Mnemonic(101)


m.add('about')
m.add('after')
m.add('again')
m.add('always')
m.add('an')
m.add('and')
m.add('any')
m.add('apple')
m.add('around')
m.add('as')
m.add('ask')
m.add('away')
m.add('baby')
m.add('back')
m.add('ball')
m.add('bear')
m.add('because')
m.add('bed')
m.add('been')
m.add('before')
m.add('bell')
m.add('best')
m.add('better')
m.add('big')
m.add('bird')
m.add('birthday')
m.add('blue')
m.add('boat')
m.add('both')
m.add('box')
m.add('boy')
m.add('bread')
m.add('bring')
m.add('brother')
m.add('buy')
m.add('by')
m.add('cake')
m.add('call')
m.add('can')
m.add('car')
m.add('carry')
m.add('cat')
m.add('chair')
m.add('chicken')
m.add('children')
m.add('christmas')
m.add('claus')
m.add('clean')
m.add('coat')
m.add('cold')
m.add('come')
m.add('corn')
m.add('could')
m.add('cow')
m.add('cut')
m.add('day')
m.add('does')
m.add('dog')
m.add('doll')
m.add('done')
m.add('dont')
m.add('door')
m.add('down')
m.add('draw')
m.add('drink')
m.add('duck')
m.add('egg')
m.add('eight')
m.add('every')
m.add('eye')
m.add('fall')
m.add('far')
m.add('farm')
m.add('farmer')
m.add('fast')
m.add('father')
m.add('feet')
m.add('find')
m.add('fire')
m.add('first')
m.add('fish')
m.add('five')
m.add('floor')
m.add('flower')
m.add('fly')
m.add('for')
m.add('found')
m.add('from')
m.add('full')
m.add('funny')
m.add('game')
m.add('garden')
m.add('gave')
m.add('girl')
m.add('give')
m.add('giving')
m.add('go')
m.add('goes')
m.add('goodbye')
m.add('got')
m.add('grass')
m.add('green')
m.add('ground')
m.add('grow')
m.add('had')
m.add('hand')
m.add('has')
m.add('head')
m.add('help')
m.add('her')
m.add('here')
m.add('hill')
m.add('him')
m.add('his')
m.add('hold')
m.add('home')
m.add('horse')
m.add('hot')
m.add('house')
m.add('how')
m.add('hurt')
m.add('if')
m.add('in')
m.add('is')
m.add('it')
m.add('its')
m.add('jump')
m.add('just')
m.add('keep')
m.add('kind')
m.add('kitty')
m.add('know')
m.add('laugh')
m.add('leg')
m.add('let')
m.add('letter')
m.add('light')
m.add('little')
m.add('live')
m.add('long')
m.add('look')
m.add('made')
m.add('make')
m.add('man')
m.add('many')
m.add('may')
m.add('me')
m.add('men')
m.add('milk')
m.add('money')
m.add('morning')
m.add('mother')
m.add('much')
m.add('my')
m.add('myself')
m.add('name')
m.add('nest')
m.add('never')
m.add('night')
m.add('not')
m.add('of')
m.add('off')
m.add('old')
m.add('once')
m.add('one')
m.add('only')
m.add('open')
m.add('or')
m.add('over')
m.add('own')
m.add('paper')
m.add('party')
m.add('pick')
m.add('picture')
m.add('pig')
m.add('play')
m.add('pull')
m.add('put')
m.add('rabbit')
m.add('rain')
m.add('read')
m.add('red')
m.add('right')
m.add('ring')
m.add('robin')
m.add('round')
m.add('run')
m.add('said')
m.add('santa')
m.add('school')
m.add('see')
m.add('seed')
m.add('seven')
m.add('shall')
m.add('sheep')
m.add('shoe')
m.add('show')
m.add('sing')
m.add('sister')
m.add('sit')
m.add('six')
m.add('sleep')
m.add('small')
m.add('snow')
m.add('some')
m.add('song')
m.add('squirrel')
m.add('start')
m.add('stick')
m.add('stop')
m.add('street')
m.add('sun')
m.add('table')
m.add('take')
m.add('tell')
m.add('ten')
m.add('thank')
m.add('the')
m.add('their')
m.add('them')
m.add('then')
m.add('these')
m.add('thing')
m.add('think')
m.add('those')
m.add('three')
m.add('time')
m.add('to')
m.add('today')
m.add('together')
m.add('top')
m.add('toy')
m.add('tree')
m.add('try')
m.add('two')
m.add('up')
m.add('upon')
m.add('us')
m.add('use')
m.add('very')
m.add('walk')
m.add('warm')
m.add('wash')
m.add('watch')
m.add('water')
m.add('way')
m.add('we')
m.add('were')
m.add('when')
m.add('where')
m.add('which')
m.add('why')
m.add('wind')
m.add('window')
m.add('wish')
m.add('wood')
m.add('work')
m.add('would')
m.add('write')
m.add('yellow')
m.add('you')
m.add('your')

print(m.follow)
print('"' + m.make('6862377') + '"')  #  "ounadrr"
print('"' + m.make('6862377') + '"')  #  "ounadrs"
print('"' + m.make('6862377') + '"')  #  "ntobers"
print('"' + m.make('6862377') + '"')  #  "ouncerr"
print('"' + m.make('6862377') + '"')  #  "ntoadrr"
print('"' + m.make('6862377') + '"')  #  "muncess"
print('"' + m.make('6862377') + '"')  #  "ounadrs"
print('"' + m.make('6862377') + '"')  #  "munadrs"
print('"' + m.make('6862377') + '"')  #  "ouncers"
print('"' + m.make('6862377') + '"')  #  "otoadrr"
Reply
#12
This is unrelated to your problem, but why do you bind so many temporary variables to the object? In Random, you keep a reference to "m", even though you never ever use it again. Same with "w" in Mnemonic.add, and "s1"/"s3" in Mnemonic.make. There's nothing wrong with just... defining variables. Just because you're in a class, doesn't mean all your variables have to be attached to the object, all you do is gobble up memory when you do that, since they can never be freed by the garbage collector until the parent object is collected (which is likely when your program ends).

And why are all the variable names such trash? "s"? "n"? "m"? "w"? "s1"? Those are all completely meaningless.
And why are you rolling your own Random, instead of just using the random module? I'm listing this one last, since there's nothing really random about what you're doing, so maybe it's part of the algorithm... in which case, calling it Random is kinda bad lol.
Reply
#13
I want to store my string of letters that can follow the the previous letter. Then I will compare this string to the string that contains all the letters associated with the next number in the phone number which appears on the US keypad.
For instance, we have a phone number "723 456 0981"
I call the letters dictionary to get the letters associated with the number 1, which is prs.
Next, I call the follow dictionary which give me all the possible letters that can follow p. Now that is why I have a bunch of "add" functions with many words, which are based on Dolch list.
possible letters that can follow r are oteioreiaimsodaeoosntaaeeiiooureeeei.
Then in my for loop,
        for i in num:
            s2 = ''
            s2 += self.follow[i]
            print("String s2 is", s2)
            print("Length of s2 is", len(s2))
            for j in s2:
                if j in self.letters[i]:
                    print("String next num is",self.letters[i])
                    s3+=j
I want to take out all the letters in string s2 that contain any letters that are in number 2, which are "abc", and concatenate to string s3. In the end, I want to call the random.choose function to choose a letter out of string s3 and put it into the string name I want to produce, which is s1.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020