Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly generate 1 or -1 (positive or negative integer)

Tags:

python

random

I wanted to generate 1 or -1 in Python as a step to randomizing between non-negative and non-positive numbers or to randomly changing sign of an already existing integer. What would be the best way to generate 1 or -1 in Python? Assuming even distribution I know I could use:

import random

#method1
my_number = random.choice((-1, 1))

#method2
my_number = (-1)**random.randrange(2)

#method3
# if I understand correctly random.random() should never return exactly 1
# so I use "<", not "<="
if random.random() < 0.5:
    my_number = 1
else:
    my_number = -1

#method4
my_number = random.randint(0,1)*2-1

Using timeit module I got the following results:

#method1
s = "my_number = random.choice((-1, 1))"
timeit.timeit(stmt = s, setup = "import random")
>2.814896769857569
#method2
s = "my_number = (-1)**random.randrange(2)"
timeit.timeit(stmt = s, setup = "import random")
>3.521280517518562
#method3
s = """
if random.random() < 0.5: my_number = 1
else: my_number = -1"""
timeit.timeit(stmt = s, setup = "import random")
>0.25321546903273884
#method4
s = "random.randint(0,1)*2-1"
timeit.timeit(stmt = s, setup = "import random")
>4.526625442240402

So unexpectedly method 3 is the fastest. My bet was on method 1 to be the fastest as it is also shortest. Also both method 1 (since Python 3.6 I think?) and 3 give the possibility to introduce uneven distributions. Although method 1 is shortest (main advantege) for now I would choose method 3:

def positive_or_negative():
    if random.random() < 0.5:
        return 1
    else:
        return -1

Testing:

s = """
import random
def positive_or_negative():
    if random.random() < 0.5:
        return 1
    else:
        return -1
        """
timeit.timeit(stmt = "my_number = positive_or_negative()", setup = s)
>0.3916183138621818

Any better (faster or shorter) method to randomly generate -1 or 1 in Python? Any reason why would you choose method 1 over method 3 or vice versa?

like image 809
Siemkowski Avatar asked Oct 18 '17 22:10

Siemkowski


1 Answers

A one liner variation of #3:

return 1 if random.random() < 0.5 else -1

It's fast(er) than the 'math' variants, because it doesn't involve additional arithmetic.

like image 125
Has QUIT--Anony-Mousse Avatar answered Nov 09 '22 04:11

Has QUIT--Anony-Mousse