0
\$\begingroup\$

I have a level manager, which creates levels and levels create platforms.

Levels can contain a variety of platforms.

I dont really understand how I can say:

I want a 10% of one platform being made, 30% chance of another, and 70% chance for anotgher and so on...

At the moment I have a method that looks like this:

   private void creationManager(float moveChance, float breakableChance, float superJumpChance)

I'm not sure how I can use these values and probabaility to determine which type of paltform is created.

Any ideas?

\$\endgroup\$
1

2 Answers 2

1
\$\begingroup\$

Another possible algorithm. This is easily expandable as well and does not have to use % of 100:

//assuming rand() returns integers
#define numberOfPlatformTypes 3
int platform[numberOfPlatformTypes];
platform[0] = 10;
platform[1] = 30;
platform[2] = 70;

int total = 0;
for(int i = 0; i < numberOfPlatformTypes; ++i)
 total += platform[i];

int r = rand()%total;
for(int i = 0; i < numberOfPlatformTypes; ++i)
{
 if(r < platform[i])
  return i;
 r -= platform[i];
}
\$\endgroup\$
3
  • \$\begingroup\$ if your rand() returns a float you can just do rand()*total instead. I am unsure what the values in your creationManager function represent so it's up to you to determine the chance for each type of platform to show up. \$\endgroup\$ Dec 3, 2013 at 22:01
  • \$\begingroup\$ Where does r come in? How does it affect the result? \$\endgroup\$
    – ThorinII
    Dec 3, 2013 at 22:01
  • \$\begingroup\$ had a typo, fixed now \$\endgroup\$ Dec 3, 2013 at 22:04
0
\$\begingroup\$

Use random numbers! :) Here is a bit of pseudo code to get you started.

// Assuming rand() returns a value between 0 and 1.
float r = rand();

if(r <= 0.1f)
 /* create platform type 1 */
else if (r <= 0.3f)
 /* create platform type 2 */
else if (r <= 0.7f)
 /* create platform type 3 */

It is amazing what you can achieve with just random numbers.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ If I'm reading this correctly, this is doing the equivalent of 10%, 20%, and 40%... \$\endgroup\$
    – ThorinII
    Dec 3, 2013 at 21:59

Not the answer you're looking for? Browse other questions tagged .