It is easy to generate a random value in python. We can use random.random() function to get a random value between 0 and 1. However, how to get a random value in [-1, 1)? In this tutorial, we will introduce you how to do.
Python random.random()
It will generate a random value in [0.0, 1.0)
For example:
import random print(random.random())
We may get: 0.844721935102244
How to get a random value between -1 and 1
It is easy, we can do as follows:
import random x = 2*random.random() -1
Run this code, we will get: -0.3964735335719407