Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Math.Random how to stop at a random number but mostly small?

Asked by 6 years ago

I know you can do

math.random(x,y)

Any number it picks between the x and the y. But I don't want it to always go up to whatever the high number is very commonly... Anyone got any ideas?

2 answers

Log in to vote
0
Answered by 6 years ago

Just a thought, not sure how effective but you could try.

local initialChoice = math.random(1,2)
if initialChoice == 1 then
    math.random(1,100) -- 50% chance this will have a chance to go up to 100
else
    math.random(1,50) -- other 50% chance that it will be capped half way.
end
0
Also, make sure you have initialChoice re-randomized every time you run the if statement. PoePoeCannon 519 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

This is how I would code it. You'll have to make adjustments and fine tune the code to meet your needs.

local function GetChance()
    local firstNumber = math.random(1,1000)
    local secondNumber = nil

    if firstNumber == 1000 then
        --(1/1000 or 0.1% chance)
        secondNumber = math.random(15,20)
        print("Number was "..firstNumber.."!")
        print("Roll the hugest number!!!  "..secondNumber)      
    elseif firstNumber <= 999 and firstNumber >= 950 then
        --(50/1000 or 5% chance)
        secondNumber = math.random(10,13)
        print("Number was "..firstNumber.."!")
        print("Roll a somewhat huge number!  "..secondNumber)       
    elseif firstNumber <= 949 and firstNumber >= 850 then
        --(100/1000 or 10% chance)
        secondNumber = math.random(7,9)
        print("Number was "..firstNumber.."!")
        print("Roll a great big number!  "..secondNumber)       
    elseif firstNumber < 849 then
        --(849/1000 or 84.9% chance)
        secondNumber = math.random(1,3)
        print("Number was "..firstNumber.."!")
        print("Roll a normal number.  "..secondNumber)      
    else
        --I would recommend keeping this else statement incase there's a goof
        print("Houston we have a problem. All numbers were not accounted for.")
    end
end

while wait(1) do
    GetChance()
end

I would also recommend keeping track of all numbers in a spreadsheet(like Excel) and making sure your percentages add up to 100.

Answer this question