'How can I create an input matrix of random floats (.00) without numpy
I'm trying to create an input matrix of random floats (with 2 decimals) without numpy. But I only get 1 decimal point. My code looks like this:
my_matrix = []
rowNo = int(input("Asignar filas:"))
colNo = int(input("Asignar columnas:"))
low = 1
high = 100
choices = list(map(float, range(low,high)))
[random.choices(choices , k=colNo) for _ in range(rowNo)]
Solution 1:[1]
The following will provide a formatted display to 2 decimal points. However, the values will not be limited to 2 decimal points.
import random
my_matrix = []
rowNo = int(input("Asignar filas:"))
colNo = int(input("Asignar columnas:"))
low = 1
high = 100
choices = list(map(float, range(low,high)))
[random.choices(choices , k=colNo) for _ in range(rowNo)]
print([f'{e:0.2f}' for e in choices]) # The floats to 2 decimal points.
Demonstration Run:
Asignar filas:3
Asignar columnas:3
['1.00', '2.00', '3.00', '4.00', '5.00', '6.00', '7.00', '8.00', '9.00', '10.00', '11.00', '12.00', '13.00', '14.00', '15.00', '16.00', '17.00', '18.00', '19.00', '20.00', '21.00', '22.00', '23.00', '24.00', '25.00', '26.00', '27.00', '28.00', '29.00', '30.00', '31.00', '32.00', '33.00', '34.00', '35.00', '36.00', '37.00', '38.00', '39.00', '40.00', '41.00', '42.00', '43.00', '44.00', '45.00', '46.00', '47.00', '48.00', '49.00', '50.00', '51.00', '52.00', '53.00', '54.00', '55.00', '56.00', '57.00', '58.00', '59.00', '60.00', '61.00', '62.00', '63.00', '64.00', '65.00', '66.00', '67.00', '68.00', '69.00', '70.00', '71.00', '72.00', '73.00', '74.00', '75.00', '76.00', '77.00', '78.00', '79.00', '80.00', '81.00', '82.00', '83.00', '84.00', '85.00', '86.00', '87.00', '88.00', '89.00', '90.00', '91.00', '92.00', '93.00', '94.00', '95.00', '96.00', '97.00', '98.00', '99.00']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
