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.
Hello,
Your idea on percentage is correct and can be done by integrating it with math.random
so we can do it like this
1 | local x = math.random( 100 ) |
2 | if x < 21 then |
3 | print ( "I have a 20 percent chance to be spawned" ) |
4 | else if x < 51 then |
5 | print ( "I spawn 30 percent of the time " ) |
6 | else |
7 | print ( "The remaining percentage is conquered by me (50 percent)" ) |
Hope this answers
You could use math.random to generate a percentage like so...
1 | local x = math.random( 1 , 100 ) |
2 |
3 | if x < = 5 then -- 5/100 chance to spawn (5% chance) |
4 | print 'spawn rare' |
5 | elseif x < = 40 then -- 40/100 chance to spawn (40% chance) |
6 | print 'spawn uncommon' |
7 | else |
8 | print 'spawn common' -- defaults to common |
9 | end |