By definition, in your first example, each one of all possible numbers is mapped to the indexes 1, 2 or 3. This is because you call :NextInteger(1, 3).
As soon as you call :NextInteger(1, 5) this entire mapping of all possible numbers is altered to the match the new indexes 1, 2, 3, 4 or 5.
Can you explain your situation a little better? Why do you need this?
Maybe there’s a different way we could solve this.
The Problem: When you use Random.new(100):NextInteger(1, #tab), you’re creating a new random number generator instance with a seed of 100. However, since the seed is the same for both cases, you get the same sequence of random numbers. This is why you’re getting “abc” for both table sizes.
Solution: To ensure different table sizes still yield the same result, you should seed the random number generator once before generating any random numbers. Here’s how you can modify your code:
Lua
local tab = {"abc", "def", "ghi"}
local rng = Random.new() -- Create a single random number generator instance
rng:Seed(100) -- Seed it with a specific value (e.g., 100)
print(tab[rng:NextInteger(1, #tab)]) -- Should consistently print "abc"
local tab2 = {"abc", "def", "ghi", "jkl", "mno"}
print(tab2[rng:NextInteger(1, #tab2)]) -- Still prints "abc"
By creating a single Random.new() instance and seeding it once, you’ll get consistent results across different table sizes. Now both cases will yield “abc” as desired.
Its for a project with a self made pet system, so everytime i put a random seed number its gonna generate an egg and pets, and i need this system to generate the pet names, i need it to get the same number of index is because so it doesnt have a different name
Really gotta love these people who use AI to answer. Really professional of them. Anyways it is probably possible I suppose:
local tab = {"abc", "def", "ghi", "jkl", "mno"}
local seed = 0
local function ChangeSeed()
seed = math.random(1,#tab)
end
ChangeSeed()
print(tab[seed]) -- idk seed so something.
print(tab[seed]) -- same value will print.
ChangeSeed()
print(tab[seed]) -- 20% chance for same value but it would probably be different.
nope thats my fault :Seed does only work in Pyton. Sometimes two langages programing has also downsides, but you are rigth i should test this code befor. Sorry for that
Have a nice day
btw AI will not replace coding only makes it easier as well as faster