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.

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

I would love to hear your suggestions.

For now I have a spawn table to which I add the items that can be spawned, but all items can be spawned at the same frequency.

Thanks for your suggestions. [import]uid: 8192 topic_id: 13122 reply_id: 313122[/import]

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 [import]uid: 10211 topic_id: 13122 reply_id: 54746[/import]

I have a question about this.

For example, imagine i had 4 enemies and i want the same frequency at start for all

local n = math.random(1,4)  
-- Spawn enemy n  

But now i have 5 enemy1 , 2 enemy2, 0 enemy3, 1 enemy4 so i want the game to spawn more enemy3 and enemy4 than enemy1 because i want almost the same frequency and avoid spawn enemy1.

For example, 25% frequency at start for 1,2,3,4
with the example above, i want 5% for 1, 10% for 2, 50% for 3, 35% for 4

How can i do that?
[import]uid: 55808 topic_id: 13122 reply_id: 62100[/import]

Hi Jose,

It’s not very clear what you mean when you say “But now i have 5 enemy1 , 2 enemy2, 0 enemy3, 1 enemy4 so i want the game to spawn more enemy3 and enemy4 than enemy1 because i want almost the same frequency and avoid spawn enemy1.”

So I’ll just ignore that and answer the rest. Just like I said above, the easiest way to think about it is probably to use a table with 100 entries and think of each one as 1 percent.

So for 5% for 1, 10% for 2, 50% for 3, 35% for 4

you’d make the table like

[lua]local myTable = {}
for i = 1, 5 do
table.insert(myTable,1,i)
end

for i = 6, 15 do
table.insert(myTable,2,i)
end

for i = 16, 65 do
table.insert(myTable,3,i)
end

for i = 66, 100 do
table.insert(myTable,4,i)
end
local pickANumber = math.random(1,100)
local yourChoice = myTable[pickANumber][/lua]

and your choice will equal 1,2,3 or 4 but with the probabilities you chose.
[import]uid: 10211 topic_id: 13122 reply_id: 62119[/import]

Sorry if my english is no good.

Imagine this:

I have a spawn code for 4 enemy types. All enemys had 25% of frequency.

enemy1: 25%
enemy2: 25%
enemy3: 25%
enemy4: 25%
total: 100%

After 1 minute, i have in screen 6 enemytype1 and 0 of the rest (no type 2 3 4)

I want to create a code to control “in-game” the frequency based on actual screen population

So:
enemy1: 10% (because now enemy1 population is 100% on screen)
enemy2: 30%
enemy3: 30%
enemy4: 30%

So in this example, i want spawn more frequently type2 type3 and type4 than type1 because i have a lot of enemytype1 and i want to change spawn frequency dynamically.

This is my objective and i dont know how to do well in code. [import]uid: 55808 topic_id: 13122 reply_id: 62391[/import]

So you are saying you really want 25% for each, and the random values just aren’t giving you that.

I think there are easier ways to deal with that than trying to monitor the values you are getting. You probably just aren’t getting a proper random value generator.

I take it from your name that spanish is probably your first language, so I’ll give this a shot in spanish/english (my spanish is rusty).
Creo que tus numeros random no estan realmente random. Cuando no usas el API randomseed(), tus numeros repiten en el mismo orden todos los veces. Antes de llamando para un numero random, puso un numero differente en randomseed(). Es possible a usar el tiempo.

Como:

[lua]randomseed( system.getTimer() )
local miNumero = math.random(0,4)[/lua]

o tambien es possible a hacer

[lua]for i = 1, 100 do
randomseed(i)
local miNumero = math.random(0,4)
end[/lua]

Espero que ayude.

Matt
W2MD [import]uid: 10211 topic_id: 13122 reply_id: 62410[/import]