Skip to content

Instantly share code, notes, and snippets.

@jeffehobbs
Last active June 14, 2024 10:29
Show Gist options
  • Save jeffehobbs/3b079b61d2a497ed5e72e7142d69ad38 to your computer and use it in GitHub Desktop.
Save jeffehobbs/3b079b61d2a497ed5e72e7142d69ad38 to your computer and use it in GitHub Desktop.
a code sample to demonstrate random sampling to estimate large numbers
# rando | jeffehobbs@gmail.com
#
# a code sample to demonstrate random sampling to estimate large numbers
import random
# globals
LARGE_NUMBER = 1000000
CHANCE = 20 # as in "1 in 20"
COUNT = 0
# main loop
i = 0
while i < LARGE_NUMBER:
number = random.randint(1, CHANCE)
if number == CHANCE:
COUNT += 1
i += 1
# calculate estimate + diff
ESTIMATE = CHANCE * COUNT
DIFFERENCE = abs(LARGE_NUMBER - ESTIMATE)
PERCENT_DIFFERENCE = str(round(((DIFFERENCE / LARGE_NUMBER) * 100), 2))
# print results
print('---')
print(f'ACTUAL LARGE NUMBER: {LARGE_NUMBER}')
print(f'CHANCE OF RECORDING: 1 in {CHANCE}')
print(f'ESTIMATE OF LARGE NUM: {ESTIMATE}')
print(f'DIFFERENCE: {DIFFERENCE}')
print(f'PERCENT DIFFERENCE: {PERCENT_DIFFERENCE}%')
print('---')
#fin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment