# Chapter 5a - Random

## Randrange​

👀

``import randomprint(random.randrange(10, 20))``

Possible random values in this range are: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19

20 is out of this range as it returns values less than that specified as the second argument.

##### Practice
• Change the code above so it chooses a number from 30-39

## Tossing a Coin​

``import randomimport timeprint('This is a simulation of a coin toss')print('Tossing coin now...\n')time.sleep(1)toss_result = random.randrange(0,2) #randrange generates a random number from the range of 0 to less than 2#check what the value of toss_result isif toss_result == 0:   print("Heads!")else:  print("Tails!")``

(i) Does the user provide any input here? No

(ii) What are the possible outputs? Heads or Tails

(iii) Which line of code requires that the random module be imported? Line 7

(iv) What are the values stored by toss_result?

0 and 1 only, comment line 8 helps in answering this question

## Exercise​

##### Exercise
• Create a program to randomnly assign someone in either `blue` or `red` team

🙋‍♀️ Sample program

## Uniform​

👀

``import randomprint(random.uniform(10, 20))``

## Dice​

``import randomimport timeprint('This program simulates the throw of a dice')print('Throwing the dice now...\n')time.sleep(1)face = random.randrange(1,7) # generate a random number in the range 1 to 6print("You got a " + str(face))``

### Exercise​

##### Practice: simulate a dice of 20 faces

• This is the program for a dice of only number 6 (not working)
• Fix it so it has 20 faces 1, 2, 3, 4... 20