Search⌘ K
AI Features

Solution Review: Predicting Election Results

Explore how to use random variables and simulations to predict election results. Learn to analyze polling data with arrays and probability, and understand the accuracy of predictions through repeated trials. This lesson consolidates your knowledge of random variables before advancing to practical applications.

We'll cover the following...

Solution 1 #

C++
import numpy as np
import numpy.random as rnd
rnd.seed(2)
people = np.zeros(1000000, dtype='int') # arthur is 0
people[490001:] = 1 # ben is 1
poll = rnd.choice(people, 1000)
polled_for_arthur = len(poll[poll == 0])
print('polled for A:', polled_for_arthur)
if polled_for_arthur > 500:
print('Octavius will predict the wrong winner')
else:
print('Octavius will predict the correct winner')

Explanation #

  • In line 5, we have created an array of zeros of length 1000000. We then replace the last 510000 ...