No matter if I do this:
1 | local biomes = { "Plains" , "Desert" } |
2 | local currBiome = biomes [ math.random( #biomes ) ] |
Or this:
1 | local biomes = { "Plains" , "Desert" , "Plains" } |
2 | local currBiome = biomes [ math.random( #biomes ) ] |
Or this:
1 | math.randomseed( os.time() ) |
2 | local biomes = { "Plains" , "Desert" } |
3 | local currBiome = biomes [ math.random( #biomes ) ] |
It will always pick the last element from the table. What am I doing wrong?
Yikes! You've stumbled on a whopper. Have a look at this question on StackOverflow.
Basically, the ROBLOX version of Lua relies on a cross-platform C implementation of rand(3)
, which, if you're on OSX, isn't the best generator.
I tested this on OSX and, you're right, the first generated number is the same regardless of the seed! To mitigate this, call math.random()
a couple of times before you actually use the output - the rest of the numbers are random, just not the first one.
Duckwit is right, but I just wanted to explain this in a little more clarity for people who don't understand. If you want to choose a random terrain based on a value, we first need to set a variable for this. Let's call it TerrainNum. To create a random value of 3 terrains, this is what is needed:
01 | TerrainNum = math.random( 1 , 3 ) -- This is saying "Pick a random number from 1 to 3" |
02 | if TerrainNum = = 1 then |
03 | -- Plains |
04 | -- Insert what you want to happen in here. |
05 | end |
06 | if TerrainNum = = 2 then |
07 | -- Desert |
08 | -- Insert what you want to happen here. |
09 | end |
10 | if TerrainNum = = 3 then |
11 | -- Swamp (or something) |
12 | -- Insert what you want to happen here. |
13 | end |
This will set TerrainNum to a random number between 1 and 3, and depending on which one it is, it will execute a different line of code.