Random from Dictionary

Hey,
so basically I’m new to dictionaries and tables and I ran into a problem with them So what is it the easiest wat for getting a random item from the dictionary:
Here is the dictionary:

local flowers = {

commons = {
	game:GetService("ReplicatedStorage").Lily, 		
	game:GetService("ReplicatedStorage").Rose,
	game:GetService("ReplicatedStorage").Dandelion
}

}
I tryed flowers.commons[math.rad(0,3)] but it seams to return nil.
Is it any way to get random item from commons or if should I make it different so it will work.
Thanks

1 Like
local Table = {1,2,3}

local Random = math.random(1,#Table)
local RandomNumber = Table[Random]

This is just a example, you need change it to your dictionary.

2 Likes

math.rad doesn’t get you a random number, but you can use Random to do that. You’ll need to set the Random seed at the start of your script so it’s properly random, but you were pretty close with your method.

The previous method was math.random, but this has some flaws which using Random solves.

local randomGen = Random.new()
(your other code here)
flowers.commons[randomGen:NextInteger(0,3)]
5 Likes

I forgot about the Random method, and yea he should use it :sweat_smile:

3 Likes

This is not the same as OP is asking, OP is referring to dictionaries

Example of a dictionary:

local dictionary = {
    ["Item1"] = game:GetService("ReplicatedStorage").Lily,
    ["Item2"] = game:GetService("ReplicatedStorage").Rose,
}

As far as getting a random one from the bunch, I believe you’d have to set it up like so:

commons = {
	["Lily"] = game:GetService("ReplicatedStorage").Lily, 		
	["Rose"] = game:GetService("ReplicatedStorage").Rose,
	["Dandelion"] = game:GetService("ReplicatedStorage").Dandelion
}
commonsTable = {"Lily", "Rose", "Dandelion"}

local randomFlower = commons[commonsTable[math.random(#commonsTable)]]
print(randomFlower.Name)
22 Likes

Thanks, it works like a charm. Next time i will think a bit for another solution :slight_smile:

1 Like

Since dictionaries are unordered you can just do

local key, value = next(t)
1 Like

Ah, how convenient. I actually have a library that contains a random key selector for a dictionary. It does what DataErased suggested but invisibly so that you don’t have to make both the dictionary and the array part yourself.

My handy function, DictionarySelectRandom, accounts for arbitrary dictionaries so all you have to worry about is writing out the dictionary. A table of keys will automatically be created and fetched from internally. You just lose the benefit of consistent seeded results as a new Random object is generated each time.

See here:

7 Likes

Was going to make an example with the same function but show it as the __call metafunction :slight_smile:

In which case you can use the magic of closures to keep a consistent random object if you wanted or poke it into the metatable.

I would avoid metatables if you don’t actually need them.

I decided not to do anything about consistent random objects mostly for the sake of interruptions (a global Random would just be the equivalent of math.random and share pseudorandom results cycles where not desired and keeping it the same would change the requirements altogether).

If you’d like to discuss my specific implementation in more detail, feel free to shoot me a PM. I had a Code Review topic on it earlier, so I’d absolutely love to hear any feedback about it. :slight_smile: