List of Different Values between 0 and 1

Rate this Tutorial:
5.00
(one vote)

Approved for Versions:4.12

Overview

This is an example of a very simple, cheap and small algorithm to create a spawning pattern where the elements spawned are spread throughout the area. There are many effective methods of doing this. This is only a quick simple method.

The Code

The Header

ASpawner.h

//...
 
UCLASS()
class NUMBERLISTTUTORIAL_API ASpawner : public AActor
{
	GENERATED_BODY()
 
public:
	// Sets default values for this pawn's properties
	ASpawner();
 
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
 
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;
 
       // The variables you need to generate different list
       int32 seed = 1; // used for selecting the number where you want to start. The values will repeat after number 180
       int32 num = 1; // Use to change the random value every-time random function is called.
 
 
};

ASpawner.cpp

void ASpawner::Tick(float DeltaSeconds)
{
    Super::Tick(DeltaSeconds);
    seed++;
    ARandom(1);
}
 
// This is the most important part. This function generates values between 0 and 1 that are perfectly distributed
// (It will take 180 instances to get a similar value and the values will be spawned spread apart.
float AActor: ARandom(int seed)
    {
        num++;
        seed = seed * num;
        float value = 0;
        bool change = false;
        if (seed % 2 == 0) { change = true; }
 
        switch (change) // more variety by intercahinging between sin and cos
        {
            case (true):
                value = (FMath::Abs(FMath::Cos(seed)));
                break;
            case (false):
                value = (FMath::Abs(FMath::Sin(seed)));
                break;
        }
 
            return value;
    }