Matteo
Matteo

Reputation: 35

Index of a random number in a list?

So I've looked for help elsewhere but couldn't find any solution. I've created a list with 51 elements and would like to pick 10 random, different positions within that list and be able to change the value at those positions(you can see this under the assignCard(person) function). Since I am using the index of the numbers I can't use methods such as random.sample or put the random numbers into a set() because I get an error message. Ive posted my code below, feel free to copy and run it to view the output, everything works except for the repeating numbers so if possible please do not change the code drastically in your answer.

""" cardGame.py
    basic card game framework
    keeps track of card locations for as many hands as needed
"""
from random import *
import random

NUMCARDS = 52
DECK = 0
PLAYER = 1
COMP = 2

cardLoc = [0] * NUMCARDS
suitName = ("hearts", "diamonds", "spades", "clubs")
rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", 
            "Eight", "Nine", "Ten", "Jack", "Queen", "King")
playerName = ("deck", "player", "computer")

def main():
  clearDeck()

  for i in range(5):
    assignCard(PLAYER)
    assignCard(COMP)

  print(cardLoc)

  print("#  " + " card      " + " location")
  showDeck()
  print("\nDisplaying player hand:")
  showHand(PLAYER)
  print("\nDisplaying computer hand:")
  showHand(COMP)

def clearDeck():
    cardLoc = [0] * NUMCARDS

def assignCard(person):
  x = random.randint(0, 51)
  cardLoc[x] = person

def showDeck():
  for i in range(NUMCARDS):
    y = rankName[i % 13]
    z = suitName[int(i / 13)]
    a = cardLoc[i]
    b = playerName[a]
    print("{:<4}{:<4} of {:<14}{:<7}".format(str(i), y, z, b))

def showHand(person):
  for i in range(NUMCARDS):
    if cardLoc[i] == person:
      print(rankName[i % 13] + suitName[int(i / 13)])

main()

Upvotes: 0

Views: 1849

Answers (1)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95948

Since I am using the index of the numbers I can't use methods such as random.sample

Sure you can:

>>> import random
>>> my_list = [0]*10
>>> my_list
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> idx = list(range(len(my_list)))
>>> idx
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> s = random.sample(idx, 5)
>>> s
[4, 0, 5, 7, 1]
>>> for i in s:
...     my_list[i] = 99
...
>>> my_list
[99, 99, 0, 0, 99, 99, 0, 99, 0, 0]

Here, just do something like:

>>> NUMCARDS = 52
>>> cardLoc = [0]*52
>>> s = random.sample(range(NUMCARDS), 10)
>>> for i in s[:5]:
...     cardLoc[i] = 1
...
>>> for i in s[5:]:
...     cardLoc[i] = 2
...

Now I'm just printing the positions:

>>> for i in range(NUMCARDS):
...     if cardLoc[i] == 1:
...         print(i)
...
10
24
25
34
41
>>> for i in range(NUMCARDS):
...     if cardLoc[i] == 2:
...         print(i)
...
14
23
28
49
50
>>>

Or, loading up your constants:

>>> NUMCARDS = 52
>>> DECK = 0
>>> PLAYER = 1
>>> COMP = 2
>>>
>>> cardLoc = [0] * NUMCARDS
>>> suitName = ("hearts", "diamonds", "spades", "clubs")
>>> rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
...             "Eight", "Nine", "Ten", "Jack", "Queen", "King")
>>> playerName = ("deck", "player", "computer")
>>>
>>> s = random.sample(range(NUMCARDS), 10)
>>> for i in s[:5]:
...     cardLoc[i] = PLAYER
...
>>> for i in s[5:]:
...     cardLoc[i] = COMP
...
>>> def showHand(person):
...   for i in range(NUMCARDS):
...     if cardLoc[i] == person:
...       print(rankName[i % 13] + suitName[int(i / 13)])
...
>>> showHand(PLAYER)
Threehearts
Ninehearts
Sixdiamonds
Ninediamonds
Twoclubs
>>> showHand(COMP)
Sevenhearts
Sevendiamonds
Sixspades
Queenspades
Kingclubs

Upvotes: 3

Related Questions