How do I generate a peudo-random number between 2 numbers using a seed?

another post asking for help yipeee

Basically, I’m making a terrain generation system in my game and I want to generate trees using noise, and they will have a 25% chance of generating based on the seed.

Example:
The current map seed is 12345.
A point on the grid, (40,20), will generate a tree because its position on the perlin noise map is bigger than 0.

if math.noise(x,y,Seed) > 0 then
    -- clone tree model and put it on the grid position
end

However, I want this tree to have a 25% chance of generating, and I want it to be based on the seed we have, so every time the map generates using the same seed, the same tree will be placed.

if math.noise(x,y,Seed) > 0 and Chance(0,100,Seed) =< 25 then
    -- clone tree model and put it on the grid position
end

Is there a way to make it work? Is it even possible on Roblox?
I’m fairly new to the whole procedural generation thing, so any advice would come handy.

As always, thanks for reading. :slight_smile:

1 Like

Please post your Chance function.

EDIT: Anyway, unless that function has a bug then you shouldn’t have to do anything special, it should still generate the same tree pattern every time with the same seed.

1 Like

Ah, I don’t mean generating a tree, I mean I want to pick a point on the map using perlin noise and then have a predictable chance which will determine if the tree will be placed.
Like, imagine that the current point’s value on the Perlin noise map is greater than 0.
Now a number between 0 to 100 will generate, but it will always be the same number with that seed.

image

Yeah that’s what I meant by tree pattern. The pattern on the map of where there are trees and where there aren’t trees.

Do you get the result you want when using the same seed, or are some things different each time?

1 Like

Ohh, sorry I was tired lol
Well the tree placement is exactly like how I want it, so the Perlin noise isn’t the problem here.
The thing is that I want the trees to have 25% chance of generating, but it will always be the same if the seed is repeated.

If you want it to generate the same number at the same position you need to combine the seed with the position. In other words just use math.noise() again, but this time multiply the x and y by some amount. So that the “size” of the gradient is way smaller.

So if this is your terrain height noise
image

and this your new tree noise

you will have trees here

~50%
or
~25%

1 Like

So, I’m assuming you want a 25% chance to place a tree at a certain height range?

I think the seed may need to be a decimal, or at least one of the arguments in math.noise must be a decimal, I might be wrong tho.

1 Like

Yep, this seems to work, but just asking, is there any way to make it work not using Perlin noise? I mean like a function or something.
If not then I’ll just mark your reply as solved :slight_smile:

perlin.noise() is a function?

But you could use something like this instead

function chance(min, max, seed, x, y)
    return Random.new(seed + x + y):NextNumber(min, max)
end

This will be more performant than perlin noise, but it might not evenly spread out the trees. You have to just play around with it to get it to somewhere you like.

May I ask how you created those images? It could be cool to know!

I did some simple image operations in paint܂net.

For the “perlin” noise I just added regular noise a couple of times, then added a slight blur. (but I should’ve just used an online perlin noise generator)

For combining the noise I first used the magic wand to select everything that is white or similar enough. Then switch to the other noise layer, copy and paste onto a new layer. Then use the magic wand again, this time just fill the selection with white on a new layer, and the background with black.