Public Group

# Spawn Rate Problem

This topic is 1748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone, so I have a little problem with my code. I'am creating a game Moving@ which is rogue-like based, and I followed tutorial of libtcod+c++, so I managed to complete that and continue developing this game, so far it was good, until more items came out.  I have an if statement with a lot of else if statements, as I read about spawn rate it's easy to use for example random(1,100)>1 has a chance of one, that is pretty simple but then a lot of if elseif comes out there is one problem. Then one of the if or elseif is correct, if statement starts from the beggining, and not even tried to check, for example I set all the random numbers to random(1,100)>50 which is 50% of spawn but still who is first in elseif statement, that one get's spawned. Here is a code:

if ( rng->getInt(0,100) < 50)

else if (rng->getInt(0,100) < 50)

else if(rng->getInt(0,100) < 50)

else if(rng->getInt(0,100) < 50)

else if(rng->getInt(0,100) < 50)

else if(rng->getInt(0,100) < 50)

else if(rng->getInt(0,100) < 50)

bunch of them

##### Share on other sites

Not sure what your problem is exactly, but from a maths point of view those dont have an equal chance.

Lets just consider:

if ( rng->getInt(0,100) < 50) spawnA();
else if (rng->getInt(0,100) < 50) spawnB();


So for A you have a 50% chance.

But for B you have a 25% chance, because what your saying there is "if not A, then with a 50% chance do B" (there was some proper syntax for that fro back when i did A Levels but i forget).

The next else if is then 12.5%, then 6.25% and so on.

So if that is not what you want, what do you want? Do you want them all to be independent (and possibly spawn multiple things at once), do you want to always spawn something, but with equal chances? Do you want to spawn something with a 50% chance, but to pick that something with equal chances?

##### Share on other sites

Hi, thanks for the fast replay, you explained me a little bit, now I have some idea about spawn rate, but not close enough, you mentioned that i can pick what I want so here is the answer that I want to pick: Spawn Something, But With Equal Chances.

##### Share on other sites

You can do something like

int r = rng->getInt(0, n); //n is amount of spawnable things

switch (r) //you can use if statements too but i thought this would work well for equal chances.

{

case 0: spawnA(); break;

case 1: spawnB(); break;

case 2: spawnC(); break;

case n: spawnN(); break;

}

##### Share on other sites

In my experience I think case is almost the same as if, but just cleaner, but I might be wrong, I will definitly try that :) Thanks for suggesting it :)

*deleted*

Edited by Dunge

• 23
• 10
• 19
• 15
• 14