Created
February 14, 2019 11:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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