Reputation: 1505
I'd like to generate random values equal to -1 or 1. I can do that by generating random integer values 0 or 1 times 2 and minus 1, but maybe there is a more simple way of doing this?
Upvotes: 2
Views: 272
Reputation: 34531
How about?
random.choice([-1, 1])
Example -
>>> from random import choice
>>> choice([-1, 1])
1
>>> choice([-1, 1])
-1
>>> choice([-1, 1])
1
>>> choice([-1, 1])
-1
>>> choice([-1, 1])
-1
>>> choice([-1, 1])
1
Upvotes: 10