Math.randomseed randomness

EDIT: the below post doesn’t quite answer the OP due to me being a bit dumb and misunderstanding the post, but I’d like to keep it here since it seems relevant enough for others browsing here!


From what I can see here, you’re looking for position-based randomness (that is, you get the same number for the same position, but random numbers at different positions)! Using randomseed is more work than it’s worth, so this solution will use Random objects for much better control.

I faced this problem in my own game Blox while writing the world generation. The new world generator had to deterministically (i.e. same every time) create seeded Random objects for each cubic chunk so that randomness can be incorporated into the terrain, pictured here with the noisy boundary between underwater sand and dirt:

This is a harder version of your problem; instead of three things to keep on top of (x, y and z), I had five!

It turns out it’s really simple to do; the core idea here is that you’re using Random objects to make more Random objects!

Here’s the snippet from Blox; I’ll explain how this works step by step:

-- A large range of integers
local LARGE_RANGE = 2^12

--[[
	Returns a new Random object for the given seed, chunk location and map generation layer.
--]]
function Randomiser:getChunkRandom(worldSeed, chunkX, chunkY, chunkZ, layer)
	local random = Random.new(worldSeed)
	
	random = Random.new(bit32.bxor(chunkX, random:NextInteger(-LARGE_RANGE, LARGE_RANGE)))
	random = Random.new(bit32.bxor(chunkY, random:NextInteger(-LARGE_RANGE, LARGE_RANGE)))
	random = Random.new(bit32.bxor(chunkZ, random:NextInteger(-LARGE_RANGE, LARGE_RANGE)))
	random = Random.new(bit32.bxor(layer, random:NextInteger(-LARGE_RANGE, LARGE_RANGE)))
	
	return random
end

We start with a Random object initialised with the world seed, which is pretty normal.

Now, we want to combine that Random object with our chunkX to create a seed for a new Random object, which (within reasonable limits) will be unique for each combination of world seed and chunk X. Change one, even slightly, and the seed will be completely different.

The way I choose to generate this ‘next seed’ is using a bitwise XOR of the chunk X and a random number generated using the previous Random object;

bit32.bxor(chunkX, random:NextInteger(-LARGE_RANGE, LARGE_RANGE))

Then, we can chain together more Random objects in the same way, by making more seeds for more Random objects, each seed being modulated in some way by each parameter in turn.

The end result is a random object initialised with a seed that depends on all of the parameters, and which is also psuedorandom itself.

This is just the implementation I used for Blox; to adapt it to your project, you’ll need to use different parameters. I leave that as an exercise to the reader - it’s not that hard to adapt at all :slightly_smiling_face:

4 Likes

Thanks! I already figured it out but your code ran faster than mine so I used it :slightly_smiling_face:. BTW, is that a picture a game on roblox or in something else

1 Like

Yup, that’s in Roblox! It’s from Blox, which is my project I’ve been working on for a long time. It’s where I took the snippet I provided from :stuck_out_tongue:

It turns out the code also helped on another project i’m working on called Space Engine.