2 votes

Impression des nombres flottants en Python pour le système de coordonnées

J'essaie d'écrire une méthode qui génère et renvoie n points aléatoires dans un espace bidimensionnel donné la largeur et la hauteur en Python. J'ai codé un algorithme mais je veux recevoir des points flottants dans le système. Voici mon code :

import random    

npoints = int(input("Type the npoints:"))
width = int(input("Enter the Width you want:"))
height = int (input("Enter the Height you want:"))

allpoints = [(a,b) for a in range(width) for b in range(height)]
sample = random.sample(allpoints, npoints)

print(sample)

Output is:

Type the npoints:4
Enter the Width you want:10
Enter the Height you want:8
[(8, 7), (3, 3), (7, 7), (9, 0)]

Comment puis-je les imprimer en tant que flottant. Par exemple : (8.75 , 6.31)

Merci beaucoup pour votre aide.

1voto

rdas Points 7360

D'abord, vous voulez prendre float comme entrée. Pour height & width remplacer le int() con float() .

Maintenant, vous ne pouvez plus générer tous les points à l'intérieur de la boîte définie par ceux-ci puisque les points flottants peuvent avoir une précision arbitraire (en théorie).

Vous devez donc trouver un moyen de générer les coordonnées séparément. Une coordonnée y aléatoire entre 0 & height peut être généré par :

<random number between 0 to 1> * height

De même pour la largeur. Et vous pouvez utiliser random.random() pour obtenir le nombre aléatoire entre 0 et 1.

Code complet :

import random

npoints = int(input("Type the npoints:"))
width = float(input("Enter the Width you want:"))
height = float(input("Enter the Height you want:"))

sample = []
for _ in range(npoints):
    sample.append((width * random.random(), height * random.random()))

print(sample)

Sortie :

Type the npoints:3
Enter the Width you want:2.5
Enter the Height you want:3.5
[(0.7136697226350142, 1.3640823010874898), (2.4598008083240517, 1.691902371689177), (1.955991673900633, 2.730363157986461)]

0voto

Corralien Points 6849

Changement a y b a float :

import random    

npoints = int(input("Type the npoints:"))
width = int(input("Enter the Width you want:"))
height = int (input("Enter the Height you want:"))

# HERE ---------v--------v
allpoints = [(float(a),float(b)) for a in range(width) for b in range(height)]
sample = random.sample(allpoints, npoints)

print(sample)

Sortie :

Type the npoints:4
Enter the Width you want:10
Enter the Height you want:8
[(1.0, 0.0), (8.0, 7.0), (5.0, 1.0), (2.0, 5.0)]

Mise à jour

Je veux un flotteur mais votre solution ne s'imprime que comme ça : 2.0 5.0
Comment pouvons-nous imprimer comme ceci : 5.56 2.75 ?

Imprimer avec 2 décimales :

>>> print(*[f"({w:.2f}, {h:.2f})" for w, h in sample], sep=', ')
(1.00, 0.00), (8.00, 7.00), (5.00, 1.00), (2.00, 5.00)

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X