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

How can one have 3 things spawn randomly but each are different in spawn rate?

Asked by 6 years ago

This is not for my purposes... this is for everybody who wants to know how to do this, since this is something I have always wondered about..

The question is: How can you spawn 3 items randomly but have them all spawn at different rates. Such as spawning by rank(Common, Uncommon, Rare, etc.).

So I have no idea how you would do this, but I was thinking of doing it by percentages but I wouldn't even know how to start, so I don't even know if that would be possible...

Again this isn't for me and my purposes, I am asking for everybody who wants to know.

2 answers

Log in to vote
1
Answered by 6 years ago

Hello, Your idea on percentage is correct and can be done by integrating it with math.random so we can do it like this

local x = math.random(100)
if x < 21 then
print("I have a 20 percent chance to be spawned")
else if x < 51 then
print("I spawn 30 percent of the time ")
else 
print("The remaining percentage is conquered by me (50 percent)")

Hope this answers

0
Oh Wow! That is so simple and yet exactly what I was asking about! Thank you for your contribution! MusicalDisplay 173 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

You could use math.random to generate a percentage like so...

local x = math.random(1,100)

if x <= 5 then                  -- 5/100 chance to spawn (5% chance)
    print 'spawn rare'
elseif x <= 40 then             -- 40/100 chance to spawn (40% chance)
    print 'spawn uncommon'
else
    print 'spawn common'        -- defaults to common
end

Answer this question