Build player strengths in the pyramid structure (PHP)

For an online game (MMORPG) I want to create characters (players) with values ​​of arbitrary strength. The stronger the characters, the less this kind of thing should exist.

Example:

  • 12 000 strength 1 player
  • 10 500 power 2 players
  • 8,500 strength 3 players
  • 6,000 strong 4 players
  • 3,000 strength 5 players

In fact, I need floating, progressive strength values ​​from 1.1 to 9.9, but for this example it was easier to explain this with whole forces.

Do you have an idea how I could code this in PHP? Of course, I will need mt_rand () to generate random numbers. But how can I achieve this pyramid structure?

Which function? Root function, exponential function, power function or logarithm function?

Thanks in advance!

It should look like this on a chart:

Pyramid Count http://img7.imageshack.us/img7/107/pyramidy.jpg

+4
source share
6 answers

You can mimic a distribution, for example, the one you described using a logarithmic function. The following will return a random force value between 1.1 and 9.9:

function getRandomStrength() { $rand = mt_rand() / mt_getrandmax(); return round(pow(M_E, ($rand - 1.033) / -0.45), 1); } 

Distribution of more than 1000 runs (where S is the force value):

 S | Count --+------ 1 - 290 2 - 174 3 - 141 4 - 101 5 - 84 6 - 67 7 - 55 8 - 50 9 - 38 

Note:

This answer has been updated to include a force value of 1.1 (which was not included earlier due to rounding) and to correct the name mt_getrandmax()

+5
source

The easiest way to do this is to provide "stripes" for a random number. In your example, you have 15 players so that you can:

 rand < 1/15, highest strength 1/15 < rand < 3/15, second highest 3/15 < rand < 6/15, third highest 6/15 < rand < 10/15, fourth highest 10/15 < rand < 15/15, lowest strength 

You can also parameterize such a function with the β€œmaximum” number of each strip that you allow, and when the strip is full, it is included in the next lower existing strip (except for the lower strip, which will be included in the next highest) the number of them with a random distribution.

Edit add from my comments:

To get the structure of a pyramid with a floating range, the best function is likely to be the logarithm. Formula:

 11 - log10(rand) 

will work (with log10 is a logarithm with base 10) as this will give ranges, for example:

 1 < rand < 10 = 9 < strength < 10 10 < rand < 100 = 8 < strength < 9 100 < rand < 1000 = 7 < strength < 8 etc. 

but rand would have to range from 1 to 10 ^ 10, which would require a lot of randomness (more than most random generators can control). To get a random number in this range, you can multiply them together. 3 random numbers could control it:

 11 - log10(rand1 * rand2 * rand3) 

with rand1 having a range of 1-10000 and rand2 and rand3 having a range of 1-1000. This would slightly distort the distribution from the correct pyramid, although (most likely, I will have a number in the center), so it may be unacceptable.

+3
source

workmad3, it seems to me, has a beginning, but there is a trick - you need to track the size of your bucket and regardless of whether they are full. The random number generator does not guarantee this. You will need to assign the values ​​of your bucket (size) and size (number of people), and let your random generator tell you which bucket the player will take out - if it is full, "overflow" to the next one below.

As for assigning bucket sizes for a given force value, this is a difficult bit (and I think you really work). The characteristics of the required distribution are critical. If you want a linear drop (which alludes to the shape of the pyramid), the line of the form

 strength = max_strength - m(number_characters) 

will work. Changing the value of m will change the speed at which the line goes down, and basically limits your maximum number of common characters. If you are looking for a more sophisticated way to reduce force values, you can use a parabolic or hyperbolic curve - they are a little more complicated, but give you very different characteristics.

+2
source

something like that

 <?php $rand = rand(1,10); switch ($rand) { case 1: echo "band 1"; break; case 2: case 3: echo "band 2"; break; case 4: case 5: case 6: echo "band 3"; break; default: echo "band 4"; break; } ?> 

Group 1 is the strongest, and Group 4 is the weakest.

Of course, this is a basic task, you will want to reorganize it to use loops instead of hard-coded switches, but you get the idea :)

0
source

generates a random number between 0 and 40,000, if it is between 0 and 12000, assign a force of 1, between 12000 and 22500, assign 2, etc.

Edit: for progressive values ​​from 0 to 10, use the square root of a random number from 0 to 100, then subtract if from 10

  • rand β†’ strengh
  • 0-1 β†’ 9.9 β†’ 9 (1%)
  • 2-4 β†’ 9 β†’ 8 (2%)
  • ...
  • 81 - 100 β†’ 1 - 0 (19%)

For results between 1.1 and 9.9, the formula will be in pseudo-code)

strength = 10 - sqrt (rand (1..79))

-1
source

In this case, the easiest way to use interest.

From your examples it will be approximately (converted to an array for ease of use later):

 $strength[1] = .3; // start with a key of 1 $strength[2] = .26; $strength[3] = .21; $strength[4] = .15; $strength[5] = .08; 

Thus, you can generate a random number with mt_rand() and divide by the maximum possible value to get a number from 0 to 1:

 $rand = mt_rand() / mt_getrandmax(); // rand is some random value between 0 and 1 

Then you can use the foreach to highlight each case:

 $comparisonPercentage = 1; $selectedLevel = count($strength); // covers the case where mt_rand() returns 0 foreach($strength as $level => $currentPercentage) { $comparisonPercentage -= $currentPercentage; if ($rand > $comparisonPercentage) { $selectedLevel = $level; break; } } // $selectedLevel contains the level you need... 

If you do it this way, you only need to change the $strength array if you need to bother with percentages.

-1
source

All Articles