mardi 25 septembre 2018

Casting to int when generating a random distribution - Python

I was messing around with graphing random distributions, and found faint, but distinct and regularly spaced, lines appear when I graphed a distribution created by int(random.random() * random.random()). Here is the code I was using:

import random
import matplotlib.pyplot as plt

x = []
y = []

for i in range(50000):
    x.append(int(100*(random.random() + random.random())))
    y.append(int(100*(random.random() + random.random())))

plt.figure(figsize = (12,12))
plt.scatter(x,y,s=3)

And this is the graph I got: Scatter-plot of distribution with distinct faint lines

If you take out the integer casting, then the whole thing looks as expected:Scatter-plot of distribution without integer casting

Oddly enough, it seems that only the x-axis is responsible for the vertical lines, as removing the cast on the x-axis gets rid of the lines. The y-axis doesn't display any faint horizontal lines, but there are a couple darker lines. Scatter-plot with integer casting removed on the x-axis only

Obviously the fainter lines have something to do with casting to integer, but why and how does that affect the overall distribution this way. Also, why is it only in the vertical direction and not applied symmetrically across both dimensions?




Aucun commentaire:

Enregistrer un commentaire