Set multiple TextLabels to different numbers

ok so i want to set like 5 textlabels to a random number with out any duplicates
ive managed to make the random number generator that has no duplicates but now im stuck on how i can set the 5 textlabels text to one of the generated numbers

here is the rng script

if Card then
	local randomSamples = {}
	for i=1,99 do --the range of pickable numbers
		randomSamples[i] = i
	end

	for i=1,5 do --how many numbers picked
		local randomPick = table.remove(randomSamples, math.random(1, #randomSamples))
		print("My random number: ", randomPick)
	end
end

well instead of printing the result, set the textlabel text to it

local randomPick = table.remove(randomSamples, math.random(1, #randomSamples))
textLabelContainer[i].Text = randomPick

name the 5 textlabels to 1,2,3,4, and 5

1 Like

ok so if i understand this right i need to make a table called textLabelContainer with the textlabel names, but how would I actually get them, im not very good with tables

this is what i attempted but it did nothing, no error

for i,v in pairs(NumList:GetChildren()) do
	if v.Name == "UIGridLayout" then return else
		table.insert(textLabelContainer, v)
	end
end 

if Card then
	local randomSamples = {}
	for i=1,99 do
		randomSamples[i] = i
	end

	for i=1,25 do
		local randomPick = table.remove(randomSamples, math.random(1, #randomSamples))
		textLabelContainer[i].Text = randomPick
	end
end

I’d probably just do

if Card then
    local chosen: {boolean} = {} -- This will hold booleans that will indicate which numbers we have used

    for i, v in NumList:GetChildren() do
        if v:IsA("TextLabel") then
            -- Pick our number
            local num = math.random(1, 99) -- adjust the bounds as necessary
            while not chosen[num] do -- pick new numbers if the number was already used
                num = math.random(1, 99)
            end
            chosen[num] = true -- mark off our number as used
            
            v.Text = tostring(num)
        end
    end
end
1 Like

You put UIGridLayouts in textLabelContainer, not the TextLabels themselves.

1 Like

Theoretically, this is a plausible code snippet, but it’s best not to handle the randomness this way.

Suppose you had a different minimum and maximum value rather than one and ninety-nine. You would have to pass all the numbers within that range, and it would then cause a table overflow. Considering the maximum number is 10^9, it could even destroy the studio because of the indexing queue.

What you should do in the script to prevent this prone issue is check if the random picked number equals one of the before-picked values in order for your script to be most performant.

local function pickRandom(min, max)
    local picked = Random.new():NextInteger(min, max)
    return picked 
end

if Card then
    local min, max = 1, 10^16
    local pickedNumbers = {}

	for i = 1, 5 do -- Loop through each text label
        local randomConfirmed = false

        repeat
	        local randomNum = pickRandom(min, max)

            if not table.find(pickedNumbers, randomNum) then
                table.insert(pickedNumbers, randomNum)
                randomConfirmed = true
	            textLabelContainer[i].Text = randomNum
            end
        until randomConfirmed
	end
end

Ignoring math.random() because it does not work with large numbers, hence replaced with Random data type. Even Random data type mitigates the issue temporarily because it cannot handle the signed large integers as well.

@1230james also wrote the intended script as well as mine.

1 Like
if v.Name == "UIGridLayout" then return else

This part would result in a bug where in the children table if it sees UIgridLayout then it would stop the execution of the rest of the code in the loop (or function if it is in one) thus you may not be able to put the rest of the containers.
Be careful with these, use continue statement instead. just wanted to give an advice here. The guys on top of me already gave a satisfactory response to the question.

2 Likes

thank you man :DD
would of been stuck on this script longer without this

1 Like

appreciate the help homie, thanks to @1230james and @Jayma1322 for their help as well,
pieced all the scripts together with a tiny bit of tinkering and it works perfectly now :heart:

3 Likes