Get Random int by range

Hi there,

Because my question was bad worded, I tried to write it better.
I hope that everybody will understand it:

I am trying to make a procedural world and i wanted to spawn cubes with a random height so I took the position and added the seed, this was the new seed but the cubes weren’t randomly placed they looked like stairs because I can’t find the error I made, I am asking you if you could help me.

Here ist the .h file

UPROPERTY(EditDefaultsOnly)
int32 iSeed = 11110;

float GetNoise(int x, int y);

UPROPERTY(EditDefaultsOnly, Category = SpawnBlock)
TSubclassOf<class ABlock> BlockClass;

FRandomStream RandomStream;

This is the .cpp file:

void ARandomWorldGenerator::GenerateTerrain()
{
	
	if (BlockClass)
	{
		FVector2D vActorLocation = FVector2D(GetActorLocation().X, GetActorLocation().Y);
		for (int32 a = 0; a < 100; a++)
		{
			FVector Location = FVector(0, 0, 0);
			Location = FVector((a / 10 * 100) + vActorLocation.X, a % 10 * 100 + vActorLocation.Y, 
            GetNoise(a / 100, a % 10));

			FActorSpawnParameters SpawnParams;
			SpawnParams.SpawnCollisionHandlingOverride = 
            ESpawnActorCollisionHandlingMethod::AlwaysSpawn;

			GetWorld()->SpawnActor<ABlock>(BlockClass, Location, FRotator(0, 0, 0), SpawnParams);

		}
	}
	
}

float ARandomWorldGenerator::GetNoise(int x, int y)
{
	RandomStream.Initialize(x*13.2 + y*2 + iSeed);
	return RandomStream.FRandRange(1, 5);
}

And in the beginplay function I call the function GenerateTerrain.

That is the result (it dosen’t look realy random):

113757-stairs.png

This is the old question:

I am trying to make a function that returns a random int and gets a seed and ervery time I call the funktion with the same seed it has to return the same value.

The code I Made:
int the .h:

FRandomStream RandomStream;
float GetNoise(int x, int y);
UPROPERTY(EditDefaultsOnly)
int32 iSeed = 11110;

and in the .cpp:

float ARandomWorldGenerator::GetNoise(int x, int y)
{
	RandomStream.Initialize(iSeed);
	return RandomStream.fRandRange(1, 5));
}

But the result isn’t realy random.
May you could help me?

Hey 143258,

I’m not sure I understand the issue. When I use the code you’ve provided, I get the same result whenever I use the same seed, but if I change the seed the result will change.

Could you explain what results you’re expecting to see?

In C++ we have rand() function, which I believe plays the same role as our RandomStream - probably RandomStream uses rand(). The problem is, until you don’t have some hardware noise generator, you can’t create on logic machine random numbers - after all computer should give always same result from same input data. ‘Random’ function in fact does some complex operations with seed numbers and outputs some numbers, changing the seed to the new value.

One of methods to ‘randomize’ our outputs is to give as seed one of variables which changes when we call program again - which is time. Long story short, we can solve our problem this way:

#include <ctime>
...
int iSeed = time(0); //time in seconds from the Beginning of The Universe, i.e. from 1970.
RandomStream.Initialize(iSeed);
...

It should work for you.

I wrote the question new I hoppe I did it better now.

I’ m realy sorry but my question was bad I tried to write it better.
but your awnser isn’t the wnser to my problem.
I am realy sory that I wrote my question so unclear. And hope that the new one is better. But thanks that you took the time to help me : )

No problem. Still, I encourage you to use rand() function instead of GetNoise :slight_smile:

I found a website with the solution(but I don’t know how it works)
this is the code I used:

int n = (int)x + (int)y * 57;
n = (n << 13) ^ n;
int nn = (n*(n*n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
return 1.0 - ((float)nn / 1073741824.0);

this is the link with the Website where I found the solution.