0% found this document useful (0 votes)
7 views7 pages

Random Numbers

The document discusses the use of random numbers in games and simulations, highlighting how Python's random number generator can be utilized to create random integers and floating points. It explains the Monte Carlo method for approximating solutions to complex problems, such as estimating the value of π. Additionally, it provides an example of a guessing game that incorporates random number generation to enhance user interaction.

Uploaded by

s150207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Random Numbers

The document discusses the use of random numbers in games and simulations, highlighting how Python's random number generator can be utilized to create random integers and floating points. It explains the Monte Carlo method for approximating solutions to complex problems, such as estimating the value of π. Additionally, it provides an example of a guessing game that incorporates random number generation to enhance user interaction.

Uploaded by

s150207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Random

Numbers/Simulations
• Games often use random numbers to make things interesting
• Rolling Dice
• Spinning a wheel
• Pick a card
• A simulation program uses the computer to simulate an activity in the
real world (or the imaginary world).
• A simulation usually involves looping through a sequence of events

05/09/2025 1
Generating Random
Numbers
• The Python library has a random number generator that produces
numbers that appear to be random
• The numbers are not completely random. The numbers are drawn
from a sequence of numbers that does not repeat for a long time
• random() returns a number that is >= 0 and < 1

05/09/2025 2
Simulating Die Tosses
• Goal:
• To generate a random integer in a given range we use the randint()
function
• Randint has two parameters, the range (inclusive) of numbers
generated

05/09/2025 3
The Monte Carlo Method
• Used to find approximate solutions to problems that cannot be precisely
solved
• Example: Approximate PI using the relative areas of a circle inside a square.
Simulate shooting a dart into a square surrounding circle of radius 1. To do
this,
• Generate random x- and y-coordinates between -1 and 1
• If the generated point lies in the circle it is a hit (when x 2+y2 ≤ 1)
• Tries are total number of tries
• π/4 = Hits / Tries
• π = 4 x Hits / Tries

 To generate a random floating point between a and b:


r = random() # 0 ≤ r < 1
x = a + ( b – a ) * r # -1≤ x < ≤ 1

05/09/2025 4
Monte Carlo Example

05/09/2025 5
Guess Game: Guess number
1 - 100
#Program simulates a guess number game Guess a number between 1 and 100: 200
#The player is asked to guess a number bewteen 1 and 100 Your guess is outside range 1-100!

from random import randint Guess a number between 1 and 100: 50


#generate a random integer 1-100 Go lower
r = randint(1,100)
Guess a number between 1 and 100: 25
Go lower
done = False
while not done:
Guess a number between 1 and 100: 10
#get user guess Go higher
userGuess = int(input("Guess a number between 1 and 100: "))
if userGuess <1 or userGuess >100 : Guess a number between 1 and 100: 15
print("Your guess is outside range 1-100!") Go higher
else:
if userGuess == r: Guess a number between 1 and 100: 18
print("Congratulation! You win.") Go lower
done = True
elif userGuess > r: Guess a number between 1 and 100: 17
print("Go lower") Congratulation! You win.
else:
print("Go higher")

05/09/2025 6
Summary
• In a simulation, you use the computer to simulate an activity.
• You can introduce randomness by calling the random number generator.

05/09/2025 7

You might also like