Create a new vector with random values.


1v_2d = py5.Py5Vector2D.random()
3# Py5Vector2D(-0.95511074, 0.29624901)
4v_3d = py5.Py5Vector3D.random()
6# Py5Vector3D(0.448865, -0.84838302, 0.28065363)
7v_4d = py5.Py5Vector4D.random()
9# Py5Vector4D(0.46938054, 0.14506664, 0.61450653, 0.61726761)
example picture for random()
1def setup():
2    py5.background(128)
3    py5.translate(py5.width / 2, py5.height / 2)
4    py5.stroke(0)
5    for _ in range(25):
6        v = 40 * py5.Py5Vector.random(dim=2)
7        py5.line(0, 0, v.x, v.y)


Create a new vector with random values. Use the dim parameter to specify if the vector should have 2, 3, or 4 dimensions.

The new vector will have a magnitude of 1 and a heading that is uniformly distributed across all possible headings for a vector with the given dimension.

When used as a Py5Vector class method, the dim parameter is required to specify what the new vector’s dimension should be. When used as a class method for the Py5Vector2D, Py5Vector3D, or Py5Vector4D child classes, the dim parameter is optional and will default to the dimension implied by the specific class. When used as a method on a vector instance, the dim parameter is also optional and will default to the vector instance’s dimension. See the example code for examples of all of these use cases.


random(dim: int, *, dtype: type = np.float_) -> Py5Vector


  • dim: int - dimension of the random vector to create

  • dtype: type = np.float_ - dtype of the random vector to create

Updated on January 16, 2022 16:51:21pm UTC