如何在python中生成负随机值

时间:2021-04-11 16:50:44

I am starting to learn python, I tried to generate random values by passing in a negative and positive number. Let say -1, 1.

我开始学习python,我尝试通过传递负数和正数来生成随机值。比如-1,1。

How should I do this in python?

我应该如何在python中执行此操作?

5 个解决方案

#1


24  

Use random.uniform(a, b)

使用random.uniform(a,b)

>>> import random
>>> random.uniform(-1, 1)
0.4779007751444888
>>> random.uniform(-1, 1)
-0.10028581710574902

#2


5  

import random

def r(minimum, maximum):
    return minimum + (maximum - minimum) * random.random()

print r(-1, 1)

EDIT: @San4ez's random.uniform(-1, 1) is the correct way. No need to reinvent the wheel…

编辑:@ San4ez的random.uniform(-1,1)是正确的方法。无需重新发明*......

Anyway, random.uniform() is coded as:

无论如何,random.uniform()被编码为:

def uniform(self, a, b):
    "Get a random number in the range [a, b) or [a, b] depending on rounding."
    return a + (b-a) * self.random()

#3


2  

if you want integer in a specified range:

如果你想要在指定范围内的整数:

print random.randrange(-1, 2)

it uses the same convention as range, so the upper limit is not included.

它使用与范围相同的约定,因此不包括上限。

random.uniform does something similar if you need float values, but it's not always clear if the upper limit is included or not

如果你需要浮点数值,random.uniform会做类似的事情,但是并不总是清楚是否包含了上限

#4


1  

Most languages have a function that will return a random number in the range [0, 1], which you can then manipulate to suite the range you need. In python, the function is random.random. So for your range of [-1, 1], you can do this:

大多数语言都有一个函数,它将返回[0,1]范围内的随机数,然后您可以操作它以适应您需要的范围。在python中,函数是random.random。因此,对于[-1,1]的范围,您可以这样做:

import random
random_number = random.random() * 2 - 1

By doubling the number we get a range of [0, 2], and by subtracting one from it, we get [-1, 1].

通过将数字加倍,得到[0,2]的范围,并从中减去1,得到[-1,1]。

#5


1  

You can also do something like this

你也可以这样做

import random
random.choice([-1, 1])

#1


24  

Use random.uniform(a, b)

使用random.uniform(a,b)

>>> import random
>>> random.uniform(-1, 1)
0.4779007751444888
>>> random.uniform(-1, 1)
-0.10028581710574902

#2


5  

import random

def r(minimum, maximum):
    return minimum + (maximum - minimum) * random.random()

print r(-1, 1)

EDIT: @San4ez's random.uniform(-1, 1) is the correct way. No need to reinvent the wheel…

编辑:@ San4ez的random.uniform(-1,1)是正确的方法。无需重新发明*......

Anyway, random.uniform() is coded as:

无论如何,random.uniform()被编码为:

def uniform(self, a, b):
    "Get a random number in the range [a, b) or [a, b] depending on rounding."
    return a + (b-a) * self.random()

#3


2  

if you want integer in a specified range:

如果你想要在指定范围内的整数:

print random.randrange(-1, 2)

it uses the same convention as range, so the upper limit is not included.

它使用与范围相同的约定,因此不包括上限。

random.uniform does something similar if you need float values, but it's not always clear if the upper limit is included or not

如果你需要浮点数值,random.uniform会做类似的事情,但是并不总是清楚是否包含了上限

#4


1  

Most languages have a function that will return a random number in the range [0, 1], which you can then manipulate to suite the range you need. In python, the function is random.random. So for your range of [-1, 1], you can do this:

大多数语言都有一个函数,它将返回[0,1]范围内的随机数,然后您可以操作它以适应您需要的范围。在python中,函数是random.random。因此,对于[-1,1]的范围,您可以这样做:

import random
random_number = random.random() * 2 - 1

By doubling the number we get a range of [0, 2], and by subtracting one from it, we get [-1, 1].

通过将数字加倍,得到[0,2]的范围,并从中减去1,得到[-1,1]。

#5


1  

You can also do something like this

你也可以这样做

import random
random.choice([-1, 1])