Skip to content

Instantly share code, notes, and snippets.

@udf
Created February 14, 2019 11:19
Show Gist options
  • Save udf/0369537cc7b0b7604f67421cb4f8e09f to your computer and use it in GitHub Desktop.
Save udf/0369537cc7b0b7604f67421cb4f8e09f to your computer and use it in GitHub Desktop.
Graphs the distribution of floating point numbers by generating bytes and interpreting them as floats
from ctypes import c_float
from os import urandom
import seaborn as sns
import matplotlib.pyplot as plt
from math import isfinite
def gen_floats(N):
out = []
while len(out) < N:
b = bytearray(urandom(4096))
for i in range(1024):
n = c_float.from_buffer(b[i * 4:]).value
if isfinite(n):
out.append(n)
return out
numbers = gen_floats(10000000)
sns.set(color_codes=True)
plot = sns.distplot(numbers)
plot.set_yscale('log')
fig = plot.get_figure()
fig.savefig('test_log.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment