是否存在符合本福德定律的随机数分布? [英] Is there a random number distribution that obeys Benford's Law?

查看:76
本文介绍了是否存在符合本福德定律的随机数分布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python有多种生成随机数分布的方法,请参见文档用于random模块.不幸的是,如果没有适当的数学背景,尤其是考虑所需的参数,就无法理解它们.

Python has a number of ways to generate different distributions of random numbers, see the documentation for the random module. Unfortunately they aren't terribly understandable without the appropriate math background, especially considering the required parameters.

我想知道这些方法中的任何一种是否能够产生随机数,且其分布符合 使用 Jan Dvorak的答案,我整理了以下代码,它似乎运行得很好.

I'd like to know if any of those methods are capable of producing random numbers with a distribution that obeys Benford's Law, and what parameter values are appropriate. Namely for a population of integers, those integers should start with a '1' about 30% of the time, '2' about 18% of the time, etc.


Using Jan Dvorak's answer I put together the following code, and it appears to work perfectly.

def benfords_range_gen(stop, n):
    """ A generator that returns n random integers
    between 1 and stop-1 and whose distribution
    meets Benford's Law i.e. is logarithmic.
    """
    multiplier = math.log(stop)
    for i in range(n):
        yield int(math.exp(multiplier * random.random()))

>>> from collections import Counter
>>> Counter(str(i)[0] for i in benfords_range_gen(10000, 1000000))
Counter({'1': 300696, '2': 176142, '3': 124577, '4': 96756, '5': 79260, '6': 67413, '7': 58052, '8': 51308, '9': 45796})

推荐答案

如果数字是从对数标度的较大范围中选择的,则本福德定律描述了一组数字的第一位数字的分布.如果准备十年内的对数均匀分布,它也将遵守法律. 10^[0,1)将产生该分布.

Benford's law describes the distribution of the first digits of a set of numbers if the numbers are chosen from a wide range on the logarithmic scale. If you prepare a log-uniform distribution over one decade, it will respect the law as well. 10^[0,1) will produce that distribution.

这将产生所需的分布:math.floor(10**random.random())

This will produce the desired distribution: math.floor(10**random.random())

这篇关于是否存在符合本福德定律的随机数分布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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