Python Forum

Full Version: random module in while loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone.
im new to python , just started before few days and i tried to made a snakes and ladders game.
as you can see in the code below i want that the program will choose a random number between 1-6 untill a player will reach to step 100 and it will be over.
but when the loop is starting every player gets the same random number the program choose at the start of the loop and not different number.
what can i do to make it different number every time to each user?
sorry for my bad english.
import random
dice = random.randint(1,6)
user1 = input('Hello,What is user1 name?:')
user2 = input('And what is the other user name?:')
user3 = input('what the user3 name whould be:)?:')
u1step = int(0)
u2step = int(0)
u3step = int(0)
while u1step < 100 or u2step < 100 or u3step  < 100 :
   start = input('Whould you like to roll the dice?: Yes Or No')
   if start == 'yes' :
       u1jump = random.randint(1, 6)
       u2jump = random.randint(1, 6)
       u3jump = random.randint(1, 6)
       u1move = u1step + u1jump
       u1step = u1move
       u2move = u2step + u1jump
       u2step = u2move
       u3move = u3step + u1jump
       u3step = u3move
   elif start == 'no' :
       print('You have ended the game')
       print(user1, 'is currently on the', u1step)
       print(user2, 'is currently on the ', u2step)
       print(user3, 'is currently on the ', u3step)
       break
   if u1step >= 100 or u2step >= 100 or u3step >= 100 :
       print('We have a winner !')
   print(user1,'is currently on the', u1step)
   print(user2, 'is currently on the ', u2step )
   print(user3, 'is currently on the ', u3step)
You need to set a seed. See https://docs.python.org/3.6/library/random.html

the syntax is
random.seed()
(Feb-03-2017, 11:33 AM)Larz60+ Wrote: [ -> ]You need to set a seed. See https://docs.python.org/3.6/library/random.html

the syntax is
random.seed()

That really shouldn't be the issue.  The random module is seeded initially regardless (with a call to the systems clock I believe).  Subsequent calls to random.randint shouldn't give the same value because of this.  Providing your own seed is generally only needed if you want to be able to have deterministic behavior.  

I think the issue here is simply typos:
       u1move = u1step + u1jump
       u1step = u1move
       u2move = u2step + u1jump
       u2step = u2move
       u3move = u3step + u1jump
       u3step = u3move
I'm guessing those u1jump's should be u2jump and u3jump for u2move and u3move respectively.
Or you can use random.SystemRandom()

rand = random.SystemRandom()
for i in range(9**999999):
    print(rand.randint(1, 2**(2**10000)))
(Feb-03-2017, 12:17 PM)wavic Wrote: [ -> ]Or you can use random.SystemRandom()
Again, not the cause of his issue.
Mekire thank you very much!
(Feb-03-2017, 12:21 PM)Mekire Wrote: [ -> ]
(Feb-03-2017, 12:17 PM)wavic Wrote: [ -> ]Or you can use random.SystemRandom()
Again, not the cause of his issue.

random.SystemRandom() gets the random number from /dev/urandom or CryptGenRandom for Windows respectively. /dev/urandom is used as a pool in cryptography. It's considered unpredictable. My snipped is not what he needs but how to use it.

random.seed(); random.randint(1,6) and rand = random.SystemRandom(); rand.randint(1,6) both produce the same output.
wavic it make no sense to use SystemRandom for a ladder game.
It's suitable for cryptographic use,not for all other stuff that need a random value.
It makes sense to me for one to know one more way to do something. It will not harm anyone.