Generate random player strengths in a pyramid structure (PHP)

前端 未结 6 1783
醉梦人生
醉梦人生 2021-01-13 19:59

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.

E

6条回答
  •  悲哀的现实
    2021-01-13 20:33

    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

提交回复
热议问题