-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtraffic_sim.R
More file actions
30 lines (24 loc) · 953 Bytes
/
traffic_sim.R
File metadata and controls
30 lines (24 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
simulate_groups <- function(mean, sd, num_cars) {
# Simulate 100 cars w/mean speed 50
cars <- rnorm(n = num_cars, mean = mean, sd = sd)
# A function to determine if a car is slower than all of the cars
# in front of it (which createa a new group of cars **behind** it)
slower_than <- function(index) {
return(cars[index] < min(cars[1:index - 1]))
}
# Apply the slower_than function to all of the cars
new_group <- lapply(2:length(cars), slower_than)
# Determine number of groups created
groups <- length(new_group[new_group == TRUE]) + 1
return(groups)
}
repeat_simulation <- function(num_sims = 10, mean = 50, sd = 5, num_cars = 100) {
# Create an empty vector to store your results
results <- vector()
# Run your simulation 100 times, and track your results
for(i in 1:num_sims) {
results <- c(results, simulate_groups(mean, sd, num_cars))
}
# Work with your results
return(hist(results))
}