# Ramdom numbers and Unicode characters
# Randomness...
# To use randomness, import the random module:
from random import *
# To create a random floating point value
# use the random() function:
for i in range(0, 10):
v = random()
print ('%.2f ' % v),
# To fit numbers into a certain
# range, we "normalize" the numbers
# v * (HI - LO) + LO
v = v * (10 - 5) + 5
print('%.2f ' % v)
# Make random integers, use the randint()
# function:
# E.G. roll two dice
for i in range(0, 10):
die1 = randint(1, 6)
die2 = randint(1, 6)
print die1, die2
# Kinda side trip: Unicode characters
# to make a unicode character:
# x = u'\u```
'
# To make a clubs symbol
clubs = u'\u2663'
diamonds = u'\u2662'
hearts = u'\u2661'
spades = u'\u2660'
# How to make a deck of cards?
suits = [clubs, diamonds, hearts, spades]
vals = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
deck = []
for s in suits:
for v in vals:
card = v + s
deck.append(card)
# Print out the deck
for i in range(0, 52):
print(deck[i]),
if (i+1)%13 == 0:
print
# Shuffle the deck - does not make a new list
shuffle(deck)
print
# Print out the deck
for i in range(0, 52):
print(deck[i]),
if (i+1)%13 == 0:
print
# Pick a card - possibly repeats
for i in range(0, 10):
c = choice(deck)
print c
# Build a hand (doesn't repeat)
hand = sample(deck, 5)
for c in hand:
print c,
```