Random Spawn with different frequency. Need your advice.

Hi,
I am trying to spawn a random object from a list of say 10 objects. But I want some items to spawn more frequently than others.

Let me give you an example.

Say I have 10 items

I want to spawn item1 1/20 times but item2 1/5 times and so on. Any suggestions on how to do that?

I would like to keep the spawning frequency as a property of the items. For example.

1
item1.spawFrequency = 1 --( Scale from 1 to 10)

All you have to do is randomly pick a number that represents your different items. To make one item more frequently picked just add an extra number that represents it into your table.

Here's an example:

you've currently got a table with 10 items. So to get and even frequency you would use
math.random(1,10) and it will spit out a random number between 1 and 10.

So say you want the first item to come up half of the time and all the rest an even amount (approximately 5%). Then you would make your table have 20 entries and use math.random(1,20), but make it so that if it spits out anything between 1 and 10, then the first item will be spawned.

Or if you want more control over it, just make the table have 100 entries and use every number in it as one percent of probability (1-50 for first item, 51-55 for second item, etc).

-Matt
W2MD

views:1487 update:2011/9/26 8:07:09
corona forums © 2003-2011