Need help making a number geneartion with Chances


local Chance = 1.2
function randomNumber(min, max)
    return min + math.random() * (max - min) / Chance
end

print(randomNumber(0,1))


How do I make this function better so it generate from 0 to 1 with decimals with chances?

I want it to output: 0.03, 0.3, 0.5, 0.2 but randomly with the randomNumber functions()

just make it so if something is selected, there is another chance that it can be unselected and replaced.

Luau has a built in random function called math.random(). To generate numbers in between 0-1, just pass in numbers that are multiplied by some power of 10 and then divide the result by said power of 10.

local randomNumber = math.random(0,1000)/1000

Would you mind elaborating on what you mean by “with chance”?

Like say if I put 10 percent there is a chance of getting a number between 0.5 and actual Number 1 and the rest is just common numbers that have low numbers

and If I were to put 50 or 80 percent there’s a high chance of getting a number 1 but I don’t know how to do it since I’m not really much of a mathematician

math.random() already gives a value between 0-1 so setting the upper range to 1000 and dividing it by 1000 is unneeded

1 Like

Good to know, I feel kind of silly for not knowing that lol

local chance = math.random()
local number = 0

if chance >= 0.6 then
    number = 1
else
    number = math.random(0, 9)/10
end

This would gives a 40% chance of the number 1 and 60% chance of a number from 0 to 0.9

this wouldnt work because math.random only accepts integers so if you put float numbers it will always round down

ty for pointing that out, in which case im sure you can do math.random(0, 9)/10 to resolve that

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