简体   繁体   English

如何在python中生成随机范围?

[英]How to generate a random arange in python?

I want to generate a random arange of 10 values in Python.我想在 Python 中随机生成 10 个值。 Here's my code.这是我的代码。 Is there any faster way or more elegant way to generate it ?有没有更快或更优雅的方式来生成它? Because the x in function lambda is actually never used.因为函数lambda中的 x 实际上从未使用过。

>>> import numpy as np
>>> import random as rd
>>> i = np.arange(0, 10)
>>> noice = map(lambda x: rd.random(), i)
>>> i
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> noice
[0.040625208925370804, 0.3979391267477529, 0.36389993607597815, 0.5222540865995068, 0.4568532903714069, 0.9028000777629279, 0.6011546913245037, 0.4779160505869603, 0.9929389966510183, 0.7688424310182902]

You can simply use np.random.rand(10) .您可以简单地使用np.random.rand(10)

See the NumpPy docs .请参阅NumpPy 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM