Question

My program is very complex, I am providing a simpler version below:

while True:#This first part asks the user if he wants to run in deterministic mode(explained later) If he does then deterministic is given a value of 1, else, it is given a value of two.
    mode= input("Would you like to start the program in deterministic mode?(y/n)")
    for i in mode:
        if i not in ('yYnN'):
            print('Please enter either a \'y\' or an \'n\', Please try again')
            break
    else:
        break

if mode=='y' or mode== 'Y':
    deterministic= 1
elif mode=='n' or mode=='N':
    deterministic= 2

Now Below is some code that uses the above code. It is an if-elif statement where if deterministic is ==1 then a function will run, and if deterministic==2 then a different function will run.

        if deterministic==2:#position,time, and energy variables will come from the function updateDisplayAllVar(....)

            position,time,energy=updateDisplayAllVar(selection,position,time,energy)
        elif deterministic==1:#position,time, and energy variables will come from the function deterministic(....)

            position,time,energy=deterministic(selection,position,time,energy)

Now I will show you the two functions from the above code. The problem lies in them I believe. The first function gives a random number, and a possibly different random number every time. The second function uses the .seed to limit the random number to the SAME "random" number each time.

def updateDisplayAllVar(selection,position,time,energy):

if selection=='w' or selection =='W':
    energy= energy
    position= position+1
    time=time+1
elif selection=='j' or selection =='J':
    energy= energy-1
    position= position+2
    time=time+1 
elif selection=='r' or selection =='R':
    energy= energy-random.randrange(1,2+1)
    position= position+random.randrange(1,4+1)
    time=time+1
elif selection=='f' or selection =='F':
    energy= energy- random.randrange(3,5+1)
    position= position+random.randrange(3,8+1)
    time=time+1



def deterministicMode(selection,position,time,energy):

    if selection=='w' or selection =='W':
        energy= energy
        position= position+1
        time=time+1
    elif selection=='j' or selection =='J':
        energy= energy-1
        position= position+2
        time=time+1 
    elif selection=='r' or selection =='R':
        random.seed(1)
        energy= energy-random.randrange(1,2+1)
        position= position+random.randrange(1,4+1)
        time=time+1
    elif selection=='f' or selection =='F':
        random.seed(3)
        energy= energy- random.randrange(3,5+1)
        position= position+random.randrange(3,8+1)
        time=time+1

Now my problem is... that, with this code, even if I do not choose to be in deterministic(no random number) mode, I still get numbers that are not random and are based on the second function. That is, even though there are two functions up there and I'm asking each function to run based on the users input... It is always deterministicMode(selection,position,time,energy) that runs. Does anyone know a fix. Please let me know if anything is unclear and I will fix it.

Was it helpful?

Solution

Just re-seed your RNG with the current system time to make it "random" again:

def deterministicMode(selection,position,time,energy):
    random.seed()  # <-- Add this line right here

    if selection=='w' or selection =='W':
        energy= energy
        position= position+1
        time=time+
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top