Python Forum
random module in while loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random module in while loops
#1
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)
Reply
#2
You need to set a seed. See https://docs.python.org/3.6/library/random.html

the syntax is
random.seed()
Reply
#3
(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.
Reply
#4
Or you can use random.SystemRandom()

rand = random.SystemRandom()
for i in range(9**999999):
    print(rand.randint(1, 2**(2**10000)))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Feb-03-2017, 12:17 PM)wavic Wrote: Or you can use random.SystemRandom()
Again, not the cause of his issue.
Reply
#6
Mekire thank you very much!
Reply
#7
(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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
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.
Reply
#9
It makes sense to me for one to know one more way to do something. It will not harm anyone.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  <while> cycle is not interrupted when using the <random>module ShityCoder 3 2,158 Sep-04-2020, 04:05 PM
Last Post: ShityCoder
  Some help with Classes and the random module please? :) AlluminumFoil 2 2,176 Jan-08-2020, 11:03 PM
Last Post: AlluminumFoil
  Can't Get Random Module! Pls Help! VictorVictus 1 7,142 Aug-24-2019, 10:20 AM
Last Post: snippsat
  Question about the Random Module Exsul 1 1,996 Mar-13-2019, 02:06 AM
Last Post: ichabod801
  Random module Python 3.6 daryl_uk 1 2,238 Oct-25-2018, 12:09 AM
Last Post: micseydel
  Random module works in idle but not terminal. ottowiser 3 5,515 Apr-11-2018, 10:59 PM
Last Post: ottowiser

Forum Jump:

User Panel Messages

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