How this random movement generation code is working?

hello, I have a code that generates Random Movement for an Actor But the problem is that I can’t Understand logic and math behind this Code:

.h file:

void RandomMovement();
float MovementRadius;

.Cpp file:

Under Tick Function:

void RandomMovement()
  MovementRadius = 5;
  AActor* Parent = GetOwner();
        Parent->SetActorLocation(Parent->GetActorLocation() + FVector(
        FMath::FRandRange(-1, 1) * MovementRadius,
        FMath::FRandRange(-1, 1) * MovementRadius,
        FMath::FRandRange(-1, 1) * MovementRadius,));

Hello. What exactly is not clear?

  • FMath::FRandRange(-1, 1) generate a num in [-1, 1]
  • So, FMath::FRandRange(-1, 1) multiplied by MovementRadius generate a num in [-MovementRadius, MovementRadius]
  • if you generate this three times and group all in FVector then you will get random FVector
  • Parent->GetActorLocation() is current position, if you add some random FVector to it you will get new position

I want to know that how it creates Random Movement:

FMath::FRandRange(-1, 1) * MovementRadius

FRandrange(-1, 1) is multiplied by Movement radius which is 5. So i want to know that how exactly this line’s Mechanism works… why they multiplied it to movement radius.

Just get paper and paint an axis line then mark [-1, 1] interval on it. FRandrange(-1, 1) is some ramdom point in this interval. If you multiply this by MovementRadius then you should mark [-MovementRadius, MovementRadius] and randomly get point from this interval. From Vector point of view if you get random point p1, then it corresponds to random vector from 0 point to this p1. So you get random Vector.

PS. If your interest is purely “why this way?” then there is no answer. There are so many ways to generate a random vector, that it is always up to you to decide which will be appropriate