Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random player strengths in a pyramid structure (PHP)

Tags:

php

random

For an online game (MMORPG) I want to create characters (players) with random strength values. The stronger the characters are, the less should exist of this sort.

Example:

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

Actually, I need floating, progressive strength values from 1.1 to 9.9 but for this example it was easier to explain it with integer strengths.

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

What function is it? Root function, exponential function, power function or logarithm function?

Thanks in advance!

It should look like this in a graph:

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

like image 351
caw Avatar asked Feb 28 '23 16:02

caw


2 Answers

You can simulate a distribution such as the one you described using a logarithmic function. The following will return a random strength 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 over 1000 runs (where S is the strength value floored):

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

Note:

This answer was updated to include a strength value of 1.1 (which wasn't included before because of the rounding) and to fix the name of the mt_getrandmax() function

like image 117
John Rasch Avatar answered Apr 27 '23 06:04

John Rasch


The simplest way to do this would be to provide 'bands' for where a random number should go. In your example, you have 15 players so you could have:

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 could also parameterise such a function with a 'max' number of each band that you allow and when the band is filled, it is subsumed into the next lowest existing band (apart from the bottom band, which would be subsumed into the next highest) to ensure only a certain number of each with a random distribution.

Edit adding from my comments:

To get a floating range pyramid structure the best function would most likely be a logarithm. The formula:

11 - log10(rand)

would work (with log10 being a logarithm with base 10) as this would give ranges like:

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

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

11 - log10(rand1 * rand2 * rand3)

with rand1 having range 1-10000 and rand2 and rand3 having range 1-1000. This would skew the distribution away from a proper pyramid slightly though (more likely to have numbers in the centre I believe) so it may not be suitable.

like image 26
workmad3 Avatar answered Apr 27 '23 08:04

workmad3