# Percentages for different platform generation [duplicate]

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?

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 = 10;
platform = 30;
platform = 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];
}

• 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. Dec 3, 2013 at 22:01
• Where does r come in? How does it affect the result? Dec 3, 2013 at 22:01
• had a typo, fixed now Dec 3, 2013 at 22:04

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.

• If I'm reading this correctly, this is doing the equivalent of 10%, 20%, and 40%... Dec 3, 2013 at 21:59