gestern sah ich im Fernsehn wie Wahlschätzungen durchgeführt werden und warum sie so genau sind: Das Prinzip ist einfach:
Angenommen man hat 10000 Stimmen, so nimmt man nur 1000 von diesen 10000 und wertet diese dann aus. Je mehr man nimmt desto genauer ist die schätzung (ist ja logisch).
Das habe ich jetzt mal als kleines script umgesetzt (in from von farben):
Code: Alles auswählen
import random
colors = list()
def createColors():
colors = list()
for nothing in range(0, 1000000, 1):
color = random.choice(["blue", "red", "yellow", "green"])
colors.append(color)
return colors
def int_to_procent(int_, maximum):
x = ((100 * int_)/maximum)
return x
def countColors():
global colors
blue = int()
red = int()
yellow = int()
green = int()
for color in colors:
if color == "red":
red += 1
elif color == "blue":
blue += 1
elif color == "green":
green += 1
elif color == "yellow":
yellow += 1
return {
"red": int_to_procent(red, 1000000),
"blue": int_to_procent(blue, 1000000),
"green": int_to_procent(green, 1000000),
"yellow": int_to_procent(yellow, 1000000)
}
def guessColors():
global colors
someColors = colors[0:1000]
blue = int()
red = int()
yellow = int()
green = int()
for color in someColors:
if color == "red":
red += 1
elif color == "blue":
blue += 1
elif color == "green":
green += 1
elif color == "yellow":
yellow += 1
return {
"red": int_to_procent(red, 1000),
"blue": int_to_procent(blue, 1000),
"green": int_to_procent(green, 1000),
"yellow": int_to_procent(yellow, 1000)
}
print "Creating Colors... ",
colors = createColors()
print "Created."
print "Guessing (with 1000 colors):\nRed: %s, Blue: %s, Green: %s, Yellow: %s" %(guessColors()["red"], guessColors()["blue"], guessColors()["green"], guessColors()["yellow"])
print "Counted Colors:\nRed: %s, Blue: %s, Green: %s, Yellow: %s" %(countColors()["red"], countColors()["blue"], countColors()["green"], countColors()["yellow"])