正太分布

概念

参考
在游戏制作的过程中,可能需要使用到这个来做随机掉落。将掉落的几率呈正太分布。

软件安装

更换pip的镜像。否则很有可能无法更新到软件。

windows 下就是在 “%HOME%/pip/pip.ini” 目录中,比如当前是administrator,那位置就是 “c:\users\administrator\pip\pip.ini”。
[global]
timeout = 30
trusted-host = pypi.douban.com
index-url = http://pypi.douban.com/simple

安装matplotlib(负责绘图)和numpy(负责数据公式)

$ pip install matplotlib
Collecting matplotlib
c:\python27\lib\site-packages\pip\_vendor\requests\packages\urllib3\util\ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
c:\python27\lib\site-packages\pip\_vendor\requests\packages\urllib3\util\ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading matplotlib-1.5.3-cp27-cp27m-win32.whl (6.0MB)
    100% |████████████████████████████████| 6.0MB 5.6kB/s
Collecting cycler (from matplotlib)
  Downloading cycler-0.10.0-py2.py3-none-any.whl
Collecting pytz (from matplotlib)
  Downloading pytz-2016.7-py2.py3-none-any.whl (480kB)
    100% |████████████████████████████████| 481kB 12kB/s
Collecting pyparsing!=2.0.4,!=2.1.2,>=1.5.6 (from matplotlib)
  Downloading pyparsing-2.1.10-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 13kB/s
  Downloading numpy-1.11.2-cp27-none-win32.whl (6.5MB)
    100% |████████████████████████████████| 6.5MB 18kB/s
Collecting python-dateutil (from matplotlib)
  Downloading python_dateutil-2.5.3-py2.py3-none-any.whl (201kB)
    100% |████████████████████████████████| 204kB 30kB/s
Collecting six (from cycler->matplotlib)
  Downloading six-1.10.0-py2.py3-none-any.whl
Installing collected packages: six, cycler, pytz, pyparsing, numpy, python-dateutil, matplotlib
Successfully installed cycler-0.10.0 matplotlib-1.5.3 numpy-1.11.2 pyparsing-2.1.10 python-dateutil-2.5.3 pytz-2016.7 six-1.10.0

python实例

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import pdb

mu, sigma = 100, 15
x = mu + sigma*np.random.randn(10000)

# the histogram of the data
# normed表示直方图规范化总面积为1,alpha表示透明度

n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', alpha=0.75)

# add a 'best fit' line
# normpdf表示在bins的每个点上用mu,sigma参数画出对应的正态点

y = mlab.normpdf( bins, mu, sigma)
z = mlab.normpdf( bins, mu, 10)

l = plt.plot(bins, y, 'r--', linewidth=1)
v = plt.plot(bins, z, 'bo', linewidth=3)

plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)

plt.show()

最终结果

C++ 11实例

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
#include <cmath>
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());

    // values near the mean are the most likely
    // standard deviation affects the dispersion of generated values from the mean
    std::normal_distribution<> d(5,2);

    std::map<int, int> hist;
    for(int n=0; n<10000; ++n) {
        ++hist[std::round(d(gen))];
    }
    for(auto p : hist) {
        std::cout << std::fixed << std::setprecision(1) << std::setw(2)
                  << p.first << ' ' << std::string(p.second/200, '*') << '\n';
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值