Change each TextLabel to a different number

Lets say I had a ScreenGui with 5 TextLabels
How could I change each of those 5 TextLabels to a random number between 1-99 without any of the number being a duplicate?

I know how to get random numbers but Im not every exactly sure on how I could change every TextLabel in a very optimized way without duplicate numbers

Here’s a psuedocode you can go off of

Create an array of numbers from 1-99
loop through the textlabels
     generate a random index based on amount of numbers in array
     assign the number into textlabel
     remove the number from array

Hope this helps :+1:

local function pickNumbers(min, max)
local taken = {};
local newNumbers = {};
for i = 1, 5, 1 do
local newNumber = math.random(min, max)
if taken[newNumber] == true then
repeat newNumber = math.random(min, max) until not taken[newNumber];
else
taken[newNumber] = true;
table.insert(newNumbers, newNumber)
end
end
return newNumbers;
end

print(pickNumbers(1,99))
--[[
Should return like:
{
[1] = "33",
[2] = "2", 
[3] = "82",
[4] = "63",
[5] = "40",
}
]]

I don’t think this is optimized since your repeat loop would go through unnecessary strains until it reaches a number that isn’t chosen. Probably best to have a preset of numbers so this doesn’t happen.

Seems to only print a max of 21 numbers

as in

{
                    [1] = 20,
                    [2] = 55,
                    [3] = 83,
                    [4] = 10,
                    [5] = 51,
                    [6] = 46,
                    [7] = 78,
                    [8] = 93,
                    [9] = 13,
                    [10] = 53,
                    [11] = 69,
                    [12] = 25,
                    [13] = 28,
                    [14] = 1,
                    [15] = 6,
                    [16] = 4,
                    [17] = 42,
                    [18] = 79,
                    [19] = 96,
                    [20] = 90,
                    [21] = 14
}

edit: now its printing at a max of 24 numbers, not really sure why
now 23, seems it changes the max numbers printed every once in a while

local function generateNumber(min, max, list)
	local num = math.random(min, max)
	
	if table.find(list, num) then
		generateNumber(min, max)
	end
end

local function randomNumbers(min, max, amount): { number }
	local numbers = {}
	
	for index = 1, amount do
		local num = math.random(min, max)
		local startAttempt = os.clock()
		
		if table.find(numbers, num) then
			repeat
				num = math.random(min, max)
			until not table.find(numbers, num) or (os.clock() - startAttempt) >= 1
		end
		
		table.insert(numbers, num)
	end
	
	return numbers
end

print(randomNumbers(1, 99, 5))
local getTextLabels = script.Parent:GetChildren() --Get the children of ScreenGui. Assuming it has 5 textlabels.

for i, label in pairs(getTextLabels) do
	
	if not label:IsA("TextLabel") then return end --Since the local script is part of getTextLabels, it will cancel that out.

	local random = math.random(1,99) --Every index gives the random number. Unlikely to duplicate.

	label.Text = random
end

Put math.random inside in pairs loop so that it wouldn’t duplicate. Is this what you meant? Or do you mean you want no absolute duplicates at all when the chance is unlikely? If so, I don’t exactly know but this is the best thing I could do:

local getTextLabels = script.Parent:GetChildren() --Get the children of ScreenGui. Assuming it has 5 textlabels.

local takenNumbers = {}

for i, label in pairs(getTextLabels) do

	if not label:IsA("TextLabel") then return end --Since the local script is part of getTextLabels, it will cancel that out.
	
	local random = math.random(1,99) --Every index gives the random number. Around 1% chance for the specific number to happen again.

	for i,number in pairs(takenNumbers) do
		if random == number then
			print("Duplicates Found!")
		end
	end
	
	table.insert(takenNumbers, random)
	
	label.Text = random
	
end

You will have to use math.random. Here is an example.

TextLabel.Text = math.random(99)
TextLabel2.Text = math.random(99)
TextLabel3.Text = math.random(99)
TextLabel4.Text = math.random(99)
TextLabel5.Text = math.random(99)

The number in the math.random is the maximum number you would want

This code should make your textlabels all have a different name without any duplicates (i may have spelt some words wrong, i cant tell cause im typing this at school xd)

local tempList = {}
for number = 1,100 do
   table.append(tempList, number)
end
-- the code above puts every number from 1-100 into a list

for i,textLabel in (yourGui):GetChildren()
    local index = math.random(1,#tempList)
    textLabel.Name = tostring(tempList[index])
    table.remove(tempList, index)
end

Basically we put every possible random number (1-100) into a list.

We loop thru all your text labels and do the following :

  1. Pick a number from the list and set it to the name of the textlabel
  2. Remove that number from the list so that other textlabels cannot use it again
  3. Repeat the same steps for the rest of your textlabels

I hope this helps!