I'm writing a card game in python 3 (version 3.8.2) and I want to choose a couple of cards from a deck of cards and give them to each of the players. For the sake of testing the code, I seed the random module by a fixed value. So I expect that each time I'm executing the code, the players would get the same cards. But apparently, things are still random.
I have a CardDeck class, which manages the card deck for each round of game. When constructed, it creates a random.Random() object for itself:
import random as rand
Class CardDeck:
def __init__(self):
// some code...
self.__random = rand.Random(100)
print(self.__random.randint(0,10)) //always prints 2
// some other code...
There's also a grab_card() method in this class, which gives the players some cards from the remaining cards in the deck (And I know, it could have been implemented better):
def grab_card(self, k):
cards = set()
for _ in range(k):
card = self.__random.sample(self.__cards.difference(set([self.__top_card])), 1)[0] # choose any card but the top card
self.__cards.remove(card)
cards.add(card)
return cards
But each time I run the program, I see that cards are given to players randomly, although using the same seed every time. This is the first run:
And here's the second run:
I can also confirm this when I test grab_card() individually:
deck = CardDeck()
somecards = deck.grab_card(5) // grab 5 cards from the deck
print(somecards) // prints random results every time I run the test
What is wrong here? Why are things still happening randomly?


Aucun commentaire:
Enregistrer un commentaire