anarres: (Default)
[personal profile] anarres
Sometimes, I do dorky things to amuse myself.

Suppose you generate 10 sets of 10 random numbers, either 0 or 1. In any given set you might get two or six or seven 1's, but it's a good bet that overall the average number of 1's per set will be about 5.

So I did this using Python (cheating a bit because I used pseudorandom numbers, but for something simple like this that should be fine). Here are my sets of numbers:

1: 0 1 0 1 0 1 0 1 1 0

2: 0 0 1 0 0 0 1 0 1 0

3: 0 1 0 0 1 0 1 0 0 1

4: 0 1 1 0 0 1 0 0 0 0

5: 1 1 0 1 1 0 0 1 1 1

6: 0 0 0 1 0 1 0 0 0 1

7: 1 1 0 1 1 0 1 1 0 0

8: 0 1 1 0 0 1 0 0 1 0

9: 1 0 0 0 0 0 0 0 0 0

10: 1 1 0 0 1 1 0 1 0 1

And here's a histogram of the results (made with the wonderful matplotlib plotting library):
(the axis labels don't show up so well: the horizontal axis is labelled "Number of 1's in a given set" and the vertical axis is labelled "Number of sets").






In fact since it's different every time I run the script, why don't I run it a few more times, and show a couple more histograms:




Histograms for 10 sets of random digits


They're all quite different. I'm going to do the same thing again, this time with 100 sets of 10 digits. I won't paste in all those numbers, but I'll show some more histograms:




Histograms for 100 sets of random digits


Again, the three graphs are not all the same, but they're a lot more similar to each-other than the first set, and they are starting to have the distinctive shape of a Gaussian curve.

If I jack the number of sets up to 100,000, my laptop hangs for a few seconds while running the script, and the resulting graphs are so similar I can't tell them apart:




Histograms for 100,000 sets of random digits


A data set that is truly random isn't at all unpredictable, in fact if you have enough of it you will just have a Gaussian distribution around some average value.

A lot of things that we describe as “random” in our lives actually aren't; instead they are governed by a complex set of interactions we don't fully understand. If life was really random it would be a lot simpler (and more boring).

I just think that is kind of neat.

Here's the Python script:


"""
Randomness.py: (pseudo)randomly generates sets of 10 binary digits (0's and 1's), and makes a histogram showing distribution of how many 1's per set.
"""
from pylab import *
import random

# Takes the total # of 1's in a set and tells which bin that set goes in by returning an
# index to the array 'histo'
def which_bin(set_total):
if set_total == 0:
return 0
elif set_total == 1:
return 1
elif set_total == 2:
return 2
elif set_total == 3:
return 3
elif set_total == 4:
return 4
elif set_total == 5:
return 5
elif set_total == 6:
return 6
elif set_total == 7:
return 7
elif set_total == 8:
return 8
elif set_total == 9:
return 9
elif set_total == 10:
return 10

n_per_set = 10 # How many numbers per set
sets = 10 # How many sets

matrix = zeros(( sets, n_per_set )) # A matrix that will hold all the 1's & 0's
histo = zeros(11) # An array to hold the heights of the histograms

for i in range(0, sets): # For every set...
set_total = 0

for j in range(0, n_per_set): # For every number in the set...
matrix[i][j] = random.randint(0,1) # Randomly set number to 0 or 1
set_total += matrix[i][j] # set_total will store the total # of 1's in the set

bin_num = which_bin(set_total) # bin_num determines which histogram bin this set goes into
histo[bin_num] += 1

for k in range(11):
print k, ": ", histo[k]
if ( histo[k]==0 ): histo[k] = 0.001 # Set the bars with height 0 to have height 0.01: otherwise
# in the bar plot they will be cut off

f = open('output.txt', 'w') # Open a file
f.write(str(matrix)) # Print matrix
f.close() # Close file

my_list = (0,1,2,3,4,5,6,7,8,9,10) # x-axis values for histogram
my_tics = (0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5) # positions for labels (at centers of bars)

# Create graph
bar(my_list, histo)
xticks(my_tics, my_list)
title("Number of 1's in a set of 10 binary digits")
xlabel("Number of 1's in a given set")
ylabel("Number of sets")
align = "edge"
show()



Date: 2010-04-10 04:41 pm (UTC)
yvi: XKCD comic: "import antigravity" (Python - Import Antigravity)
From: [personal profile] yvi
Oh, another person using Python :) Welcome to Dreamwidth!

Date: 2010-04-27 12:59 am (UTC)
foxfirefey: Fox stealing an egg. (mischief)
From: [personal profile] foxfirefey
They should make a post to [community profile] python!
Edited Date: 2010-04-27 01:01 am (UTC)

Profile

anarres: (Default)
anarres

July 2012

S M T W T F S
1234567
891011121314
151617 18192021
222324 25262728
293031    

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 18th, 2025 07:10 pm
Powered by Dreamwidth Studios