Spawn Rate Problem

Started by
4 comments, last by Dunge 10 years, 8 months ago

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

So Please Help Me :)

Check out my blog or twitter for news how I am doing in the project "Learn Programming in 2 Years!". You can follow my progress, and help me! :)

My Projects:

"Learn Programming In 2 Years"

"Windows Tutorials"

"Ubuntu Tutorials"

My Games:

Moving@ IndieDB: http://www.indiedb.com/games/moving-rl

My Links:

My Blog: http://www.thescriptan.blogspot.com

My Twitter: https://twitter.com/TheScriptan

Advertisement

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?

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.

Check out my blog or twitter for news how I am doing in the project "Learn Programming in 2 Years!". You can follow my progress, and help me! :)

My Projects:

"Learn Programming In 2 Years"

"Windows Tutorials"

"Ubuntu Tutorials"

My Games:

Moving@ IndieDB: http://www.indiedb.com/games/moving-rl

My Links:

My Blog: http://www.thescriptan.blogspot.com

My Twitter: https://twitter.com/TheScriptan

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;

}

o3o

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 :)

Check out my blog or twitter for news how I am doing in the project "Learn Programming in 2 Years!". You can follow my progress, and help me! :)

My Projects:

"Learn Programming In 2 Years"

"Windows Tutorials"

"Ubuntu Tutorials"

My Games:

Moving@ IndieDB: http://www.indiedb.com/games/moving-rl

My Links:

My Blog: http://www.thescriptan.blogspot.com

My Twitter: https://twitter.com/TheScriptan

*deleted*

This topic is closed to new replies.

Advertisement