Get the same number from randomization even the max number are different

  1. What do you want to achieve?
    How do i get the same number from randomization even the max number are different?

  2. What is the issue?
    I’m trying to get the same index number of a table from a Seed Random but with different max number, so for example like :

local tab = {"abc", "def", "ghi"}
print(tab[Random.new(100):NextInteger(1, #tab)]) -- abc
local tab = {"abc", "def", "ghi", "jkl", "mno"}
print(tab[Random.new(100):NextInteger(1, #tab)]) -- abc, i want it to be still abc
  1. What solutions have you tried so far?
    Doing something with the Random
1 Like

It’s impossible.

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.

1 Like
  1. 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.
  2. 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.

Try this

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

It’s not possible unfortunately, and most likely what @Zaberadler explained above is not going to work.

yeah sorry but i think its ai created since :Seed function doesnt exist :pensive:

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.

I suppose this is what you want? Idk.

1 Like

nope thats my fault :Seed does only work in Pyton. :slight_smile: 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

1 Like
local tab = {"abc", "def", "ghi", "jkl", "mno"}
local randomIndex = math.random(1, #tab)
local randomElement = tab[randomIndex]
print(randomElement)