Computer Simulation
What is Computer Simulation?
Computer simulation is the process of creating a mathematical model of a real-world system and using computers to predict how the system will behave under different conditions.
Types of Simulations
- Discrete Event Simulation
- Continuous Simulation
- Monte Carlo Simulation
- Agent-based Modeling
- Physical Simulation
Monte Carlo Simulation
monte_carlo.spdl
# Monte Carlo π estimation
points_inside <- 0
total_points <- 100
REPEAT total_points TIMES {
x <- RANDOM(0, 1)
y <- RANDOM(0, 1)
distance <- (x * x + y * y) ^ 0.5
IF (distance <= 1) {
points_inside <- points_inside + 1
}
}
pi_est <- 4 * points_inside / total_points
DISPLAY("Points: 100, π ≈ ")
DISPLAY(pi_est)
Monte Carlo Simulation
monte_carlo.spdl
# Dice roll simulation
dice_results <- [0, 0, 0, 0, 0, 0]
rolls <- 1000
REPEAT rolls TIMES {
roll <- RANDOM(1, 6)
dice_results[roll] <- dice_results[roll] + 1
}
REPEAT 6 TIMES {
i <- n
probability <- dice_results[i] / rolls
DISPLAY("Face ")
DISPLAY(i)
DISPLAY(": ")
DISPLAY(dice_results[i])
DISPLAY(" times (")
DISPLAY(probability)
DISPLAY(")")