Skip to content Skip to sidebar Skip to footer

Simple Random Vector Function Returns Same Direction More Than Once

So I made this simple function to return a random vector of magnitude 1 using pygame, basically a random vector that is then normalized (giving a direction). However, when I run a

Solution 1:

An alternative solution is to generate a random angle in range [0, 2*PI]. Compute the vector from the angle:

angle = random.uniform(0, math.PI*2)
vector = Vector2(math.cos(angle), math.sin(angle))

or

vector = Vector2(1, 0).rotate_rad(random.uniform(0, math.PI*2))

Solution 2:

Implemented the function inside a class for objects which I wanted to give that random direction and created n objects inside a for-loop. So implementing it as an object attribute alongside others instead of finding a random vector 3 times in a row fixes it...

Post a Comment for "Simple Random Vector Function Returns Same Direction More Than Once"