def makeDescendingUnitArray(length: int):
if (not isinstance(length, int)) or (length < 1):
raise ValueError("Array Length must be an int with a value of at least 1")
if length == 1:
return [1]
else:
constant = 1
output = list()
for x in range(length - 2):
constant /= 2
output.append(constant)
return output + [2*constant/3, constant/3]
for arrayLength in range(1, 10):
array = makeDescendingUnitArray(arrayLength)
print(array)
import random
# create a non-normalized ascending list of numbers
lst = sorted(random.uniform(0,10) for _ in range(10))
# compute the sum
temp_sum = sum(lst)
# now divide each member by the sum to normalize the list
lst = [i/temp_sum for i in lst]
print(lst,sum(lst))
如果你想要一个数学上可预测的一个线性函数,那么有一个。。。 (循环显示它的外观)
这将产生以下输出。注意,这和所有其他算法一样容易受到浮点舍入错误的影响。有一些更好的算法和一些更差的算法,但在某些时候,它们都会有一些错误。在
^{pr2}$如果你想要一些数学上可以预测的东西。。。在
生成以下数组。。。在
^{pr2}$好吧,让我们从0到10随机抽取10个数字,然后对它们进行排序。然后计算sum并重新生成一个新列表,其中每个元素除以该总和:
一个输出可以是:
^{pr2}$由于浮点不准确,总和可能不完全是1,但将非常接近。在
相关问题 更多 >
编程相关推荐