Math.randomseed randomness

The most random I could think of is math.randomseed(tick()). Same seed makes it very consistent.


Why don’t you try the Random feature?

2 Likes

The x, y, z values are basically set in intervals of 50 so the areas are evenly spaced.

Thats exactly why you are getting similar results. You are setting them to the same value rather than making them random (Which still won’t fix your issue), then when feeding them into the math.randomeseed() function, the results are going to stay the same, because you are not setting them to different values every time you run the game, even if you set them to math.random(min, max), you are still going to get the same results, because the seed has not been changed before we set those values. Let me give you a snippet of code that I have which changes it’s seed in such a way, that is almost completely random.

Does the Random feature work in the same way as random seed where I can put it before math.random and it will generate the same number per seed?

Here is what I use to get almost completely random results:

local Seed = (math.random() % math.random() * tick())
math.randomseed(Seed)
print(math.random())

I need the world to be the same every time but each area has to be different from the other areas. What I have so far is that the script is generating the same world each time but all the areas look very similar.

Yes, but why are you beating about the bush on random seed? Just keep one seed and the randomness will be fine. Seriously.

1 Like

That’s the problem, I expected it to look random but all the areas look very similar.

You don’t need to generate new seeds, you need to generate new random numbers rather than using new seeds to generate. Even Minecraft used one seed and that’s it.

1 Like

That Random.new thing seemed to fix the problem. All the areas are looking different now, thanks. I was using one seed before but that wasn’t working out so I generated seeds per area but Random.new seems to work so I will switch back to a single seed.

If you need predicable randomness then using a seed for each instance is the way to go.
I have done this in the past by having a table of seeds where each has been tested by me and proved to be playable and then use a random selector for the index of that table.
Makes for easy testing if a player has problems with a specific area generated from a specific seed.
I hope this helps.

I was using that with randomseed but all the areas were looking similar so I switched it to Random.new instead and its working much better.

1 Like

You can use math.noise to generate these random seeds based on coordinates.

I see. I am not sure if you had tested each seed to ensure the result was playable and sufficiently different to be a good one. If you have I apologise.
I am assuming you are using the random function in build or selection processes within the game and wanted the game to look and feel different.

I am now using math.noise and Random.new together and I’m getting the exact results I was looking for, thanks everyone

1 Like

Don’t use randomseed. It changes the seed globally so if you use math.randomseed often, now you have several scripts clashing for a seed and using the same one when running operations. Random allows you to assign a seed to a Random object that other code will be unable to interfere with. It also allows results branching through the Clone method; seed is preserved.

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.