How to get how many random need to be done to get specific number

like math.random(0,100)

print output : 51 times to get number 20

1 Like

uhh

for i = 1, 500 do -- taking anything above 500 attempts is unlikely, or more likely, math.randomseed has been set
	local randomNumber = math.random(1,100)
	if randomNumber ~= 20 then continue end
	print("it took",i,"attempts to roll the number 20")
end

…?

1 Like

can you do like more than 500? like forever

yes you can just make the

for i = 1, 500 do

make the 500 bigger

1 Like

yea like what @SubLimitless said but i wouldnt recommend it incase math.randomseed is set bc your game would just crash

1 Like

I really do not understand your question but i made something that might be useful

local function GetNumber(target)
    local attempts = 0
    repeat
        attempts = attempts + 1
        local RandomNumber = math.random(0, 100) 
        wait(1)
    until RandomNumber == target
    return attempts
end

Example of use:
local Attempts = GetNumber(20) -- This will repeat forever until get the number
 print("It took " .. Attempts .. " attempts to get the number 20") -- Stop the function when got the number
2 Likes


It can take much or less time depending of the “luck”

1 Like

This is working better thanks!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.