python中0到1之间的随机数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33359740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random number between 0 and 1 in python
提问by Talia
I want a random number between 0 and 1, like 0.3452. I used random.randrange(0, 1)
but it is always 0 for me. What should I do?
我想要一个介于 0 和 1 之间的随机数,例如 0.3452。我用过,random.randrange(0, 1)
但对我来说总是 0。我该怎么办?
采纳答案by jh314
You can use random.uniform
您可以使用 random.uniform
import random
random.uniform(0, 1)
回答by John La Rooy
random.random()
does exactly that
random.random()
正是这样做的
>>> import random
>>> for i in range(10):
... print(random.random())
...
0.908047338626
0.0199900075962
0.904058545833
0.321508119045
0.657086320195
0.714084413092
0.315924955063
0.696965958019
0.93824013683
0.484207425759
If you want reallyrandom numbers, and to cover the range [0, 1]:
如果你想要真正的随机数,并覆盖范围 [0, 1]:
>>> import os
>>> int.from_bytes(os.urandom(8), byteorder="big") / ((1 << 64) - 1)
0.7409674234050893
回答by matsib.dev
I want a random number between 0 and 1, like 0.3452
我想要一个介于 0 和 1 之间的随机数,例如 0.3452
random.random()
is what you are looking for:
random.random()
是你要找的:
From python docs:random.random()Return the next random floating point number in the range [0.0, 1.0).
来自 python 文档:random.random()返回范围 [0.0, 1.0) 中的下一个随机浮点数。
And, btw, Why your try didn't work?:
而且,顺便说一句,为什么你的尝试没有奏效?:
Your try was: random.randrange(0, 1)
你的尝试是: random.randrange(0, 1)
From python docs:random.randrange()Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn't actually build a range object.
来自 python 文档:random.randrange()从 range(start, stop, step) 中返回一个随机选择的元素。这相当于choice(range(start, stop, step)),但实际上并没有构建一个范围对象。
So, what you are doing here, with random.randrange(a,b)
is choosing a random element from range(a,b)
; in your case, from range(0,1)
, but, guess what!: the only element in range(0,1)
, is 0
, so, the only element you can choose from range(0,1)
, is 0
; that's whyyou were always getting 0
back.
所以,你在这里做的random.randrange(a,b)
是从 中选择一个随机元素range(a,b)
;在您的情况下,来自range(0,1)
,但是,您猜怎么着!: 中的唯一元素range(0,1)
是0
,因此,您可以从中选择的唯一元素range(0,1)
是0
;这就是为什么你总是0
回来。
回答by Hackaholic
you can use use numpy.randommodule, you can get array of random number in shape of your choice you want
您可以使用numpy.random模块,您可以获得您想要的形状的随机数数组
>>> import numpy as np
>>> np.random.random(1)[0]
0.17425892129128229
>>> np.random.random((3,2))
array([[ 0.7978787 , 0.9784473 ],
[ 0.49214277, 0.06749958],
[ 0.12944254, 0.80929816]])
>>> np.random.random((3,1))
array([[ 0.86725993],
[ 0.36869585],
[ 0.2601249 ]])
>>> np.random.random((4,1))
array([[ 0.87161403],
[ 0.41976921],
[ 0.35714702],
[ 0.31166808]])
>>> np.random.random_sample()
0.47108547995356098
回答by hpaulj
RTM
RTM
From the docs for the Python random
module:
来自 Pythonrandom
模块的文档:
Functions for integers:
random.randrange(stop)
random.randrange(start, stop[, step])
Return a randomly selected element from range(start, stop, step).
This is equivalent to choice(range(start, stop, step)), but doesn't
actually build a range object.
That explains why it only gives you 0, doesn't it. range(0,1)
is [0]
. It is choosing from a list consisting of only that value.
这就解释了为什么它只给你 0,不是吗。 range(0,1)
是[0]
。它从仅包含该值的列表中进行选择。
Also from those docs:
也来自这些文档:
random.random()
Return the next random floating point number in the range [0.0, 1.0).
But if your inclusion of the numpy
tag is intentional, you can generate many random floats in that range with one call using a np.random
function.
但是,如果您numpy
有意包含标记,则可以使用np.random
函数通过一次调用在该范围内生成许多随机浮点数。
回答by infinito84
random.randrange(0,2) this works!
random.randrange(0,2) 这有效!
回答by M T Head
My variation that I find to be more flexible.
我发现我的变体更加灵活。
str_Key = ""
str_FullKey = ""
str_CharacterPool = "01234ABCDEFfghij~-)"
for int_I in range(64):
str_Key = random.choice(str_CharacterPool)
str_FullKey = str_FullKey + str_Key