在金字塔结构(PHP)中产生随机的玩家优势 [英] Generate random player strengths in a pyramid structure (PHP)

查看:100
本文介绍了在金字塔结构(PHP)中产生随机的玩家优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在线游戏(MMORPG),我想创建具有随机强度值的角色(玩家).字符越强,这种种类就应该越少存在.

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.

示例:

  • 12,000名力量1名玩家
  • 10,500名力量2名玩家
  • 8,500名实力3名玩家
  • 6,000名实力4名选手
  • 3,000名实力5名选手

实际上,我需要从1.1到9.9的浮动渐进强度值,但对于此示例,使用整数强度更容易解释它.

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.

您知道如何用PHP编写代码吗?当然,我将需要mt_rand()来生成随机数.但是我如何实现这种金字塔结构?

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?

提前谢谢!

它在图形中看起来应该像这样:

It should look like this in a graph:

金字塔图http://img7.imageshack.us/img7/107/pyramidy .jpg

推荐答案

您可以模拟分布,例如使用对数函数描述的分布.以下内容将返回1.1到9.9之间的随机强度值:

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);
}

分布超过1000次(其中S是下限的强度值):

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

注意:

此答案已更新为包括强度值1.1(由于四舍五入而未包括在内),并修复了

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

这篇关于在金字塔结构(PHP)中产生随机的玩家优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆