I Haz Codes
print |
random |
. |
randint |
( |
low |
, |
high |
) |
Random numbers are produced in python by using the random module. The video below demonstrates how to use one function from the random module, randint(a,b). Random numbers on a computer are actually the product of a repeatable algorithm As such they can not produce truly random numbers. However they are random enough for most programs.
The code below is from the video –
import random
dice = random.randint(1,6)
if dice == 1:
print ("you win")
else:
print ("you lost with the roll of ", dice)
function – A named block of code which can be called to perform another task. They can be pre-written in modules or created by the programmer.
Module – A pre-written selection of related functions collected together for use by other programmers.
Python docs for the random module
More detail on random functions
1. Write a program which will display a random number between 20 and 30.
2. Write a program which will roll two six-sided die and display their totals.
3. Write a program which will allow the user to pick a number between 1 and 1000. The computer will then select a random number between 1 and the number entered.
Leave a Reply