# How do i extract a number from a number value

So i have been trying recently to select a number from a number value so for example we have “30542326” i want like to select a specfic number from it, (i want it to work on randomized numbers not specific numbers cuz the “30542326” will be changed randomly) so for example if i extract the third number i get 5 and when the value randomize like for example to “348413” i want to get t he random 8 and use it in a script, couldnot find any helpful articles and didnot find any answers on the dev forum.

You could try splitting the number and then randomly selecting a number from that table.

``````local numeric_value = "123456" -- String of number.
local values_table = numeric_value:split()
local random_index = math.random(1,#values_table)
local random_value = values_table[random_index]
``````

Something like that, I don’t know if the above code actually works since I didn’t test it but you get the idea.

well i think your idea can work but im not sure of how i can select a specfic one do i just do (#values_table - 1) to get it?

Try using `string.sub` or the method that ComplicatedParadigm posted.

``````local n = 1234567
local str = tostring(n) -- im lazy to put a ' around the number

local part = tonumber(str:sub(5, #str))
print(part) --> 567
``````

If you don’t want it to go to the length of the number then replace the second argument in sub with the number you want it to end with.

2 Likes