This question already has an answer here:
i'm a beginner in python so i'm sorry if this question is bad but i'm trying to write a simple script to calculate the distance of some points from the origin but when i went to test it with some random data i got a weird result: the x and y values got the same value from random() no matter what i did. here is as minimal an example as i could make of the issue:
from random import random
x = y = [None]*5
for i in range(5):
x[i] = random()*10
y[i] = random()*10
print("x: ", x[i])
print("y: ", y[i])
dist = []
for d in range(len(x)):
dist.append((x[d]**2 + y[d]**2)**0.5)
print("d: ", dist[d])
which i would expect to print 5 random values for x and 5 different random values for y and then the distance from each pair to the origin but instead it prints the same value for each x and y, like this
x: 4.0208682354265655
y: 4.0208682354265655
x: 3.923508926640271
y: 3.923508926640271
x: 6.854641339548273
y: 6.854641339548273
x: 0.5508569083735182
y: 0.5508569083735182
x: 0.312088919076029
y: 0.312088919076029
d: 5.686366391055424
d: 5.548679536146577
d: 9.693926747592448
d: 0.7790293107487427
d: 0.44136038202367955
i thought maybe it was the random module because i've heard before not to use it and to use numpy's instead but it happened with either. what is going on here?
Aucun commentaire:
Enregistrer un commentaire