Need help randomizing/randomly picking words

So I’m make this game where it chooses random words I have a system in place right now but It keeps getting the same set of words I know rng have a work around (the words dont have different raritys) where the scarmble a table and get it like that, any help appreciated. :+1:

1 Like

A simple, but effective system:

listOfWords = {"apple", "banana", "crunchy", "do"}

local function getRandomWord()
	return listOfWords[math.random(1, #listOfWords)]
end

while task.wait(1) do
	print(getRandomWord())
end

If your wondering how it works:

  • Uses math.random() to pick a random index from the list.
  • 1 is the minimum amount you can have in the table, and #listOfWords returns the amount of items/index range that your table has (ie: playercount = #game.Players:GetPlayers())

tried this I think there something to do with randomseed that makes it repeat the same set of words

Code Currently:

function WordListModule.getRandomWordFromTier(tier)
	local tierWords = wordTiers[tier]
	if tierWords then
		local randomIndex = math.random(1, #tierWords)
		return tierWords[randomIndex]
	else
		return nil
	end
end

What does your list look like?

list = {
[“Teir1”] = {“words”}
}
etc

What happens when you call that function? Does it return nil?

no it just gives the same set of words some words un touched even in 100

What do you mean by the same set of words?

It would pick a set of words in the set eg list [word , worrddd ,woooord , dog]
mostly words would be chosen even tho each of them have same chance

I’m not sure if this would help, but you could try using Random.new() to generate a new seed, instead of using math.random().

local r = Random.new()
local wordChosen = wordList[r:NextInteger(1, #wordList)]

maybe instead of

try Random.new():NextInteger(1, #tierwords)

This worked fine for me, it picked a random word as it should:

local WordListModule = {}
local wordTiers = {
	["Tier1"] = {"word1", "word2", "word3"}
}


function WordListModule.getRandomWordFromTier(tier)
	local tierWords = wordTiers[tier]
	if tierWords then
		local randomIndex = Random.new():NextInteger(1, #tierWords)
		return tierWords[randomIndex]
	else
		return nil
	end
end

return WordListModule

I’m not sure, but from what I read you seem to have a problem with the script choosing the same set of words? If you alwasy supply the function with for example “Tier1” it’s always going to choose words from that set of words

That is what I recommended : /

1 is the minimum amount you can have in the table

That’s wrong. {} this is an empty table containing 0 data.

I think you meant to say that array index starts from 1

Our posts were a literal minute apart, I didn’t see what you replied dude