简体   繁体   English

我编写了一个简单的随机数生成器,如何绘制我编写的函数的分布图?

[英]I wrote a simple random number generator, how can I graph the distribution of the function I wrote?

This is my first time writing a random number generator and I was just messing around to see what I can do with just random formulas. 这是我第一次编写随机数生成器,而我只是在四处弄乱,看看我可以用随机公式做什么。

I am curious, however, with how bias my function is and with the distribution of the function (between 1 through 9). 但是,我对我的功能有多偏心以及功能的分布(在1到9之间)感到好奇。 Here is my unnecessarily long code: 这是我不必要的长代码:

import time

class Random:
    """random generator"""

    def __init__(self):
        """ Random()-> create a random number generator with a random seed
        a seed is needed in order to generate random numbers"""
        self.seed = time.time()

    def random(self):
        """ Random.random() -> get a random number using a formula"""
        self.seed =(((int(self.seed)*129381249123+2019383)**0.74123)/517247) % 288371

    def get_ran_num(self):
        """ Random.get_ran_num() -> return a random integer from 1 thru 10"""
        self.random()
        return int(list(str(int(self.seed)))[3])


ranNum = Random()

It would be great if there exist some tools that can take a random function and then run it some thousand times and then graph the distribution of it. 如果有一些工具可以执行随机函数,然后运行数千次,然后以图形方式显示其分布,那就太好了。

Thank you in advance 先感谢您

p/s: How can I improve my RNG and make it even random-er? p / s:如何改善RNG并使其更加随机?

I would try random.rand and matplotlib. 我会尝试random.rand和matplotlib。

import numpy as np
import matplotlib.pyplot as plt


N = 50
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2  # 0 to 15 point radiuses

plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.show()

Something like this? 像这样吗

Edit: Are you trying to generate a psuedo random number? 编辑:您是否要生成伪随机数? You'd need a seed for the shift register anyway, so in that respect, I am not sure if it would be completely random. 无论如何,您都需要为移位寄存器提供种子,因此在这方面,我不确定它是否将是完全随机的。

If you just want a visual representation, you can pretty easily use 如果您只需要视觉表示,就可以轻松使用

import matplotlib.pyplot as plt
# Random class here
ranNum = Random()
randNums = [ranNum.get_ran_num() for _ in range(100)]
nums = list(range(len(randNums)))
plt.plot(nums, randNums, 'ro')
plt.show()

Here it is for 100 random numbers: 这是100个随机数: https://i.gyazo.com/bd3f11fb80de18797dc888138d5e5113.png

However, I'm getting an IndexError when I go to higher ranges. 但是,当我进入更高的范围时会遇到IndexError You should probably fix the actual algorithm for whatever is causing that problem, but the way I put a band aid on it was: 您可能应该修复导致该问题的实际算法,但是我对它施加创可贴的方式是:

def get_ran_num(self):
    """ Random.get_ran_num() -> return a random integer from 1 thru 10"""
    retval = None
    while True:
        try:
            self.random()
            retval = int(list(str(int(self.seed)))[3])
            break
        except IndexError as e:
            continue
    return retval

Here's a plot for 100,000 random numbers, which is pretty good. 这是一个100,000个随机数的图,非常好。 Contiguous lines where none is noticeably more dense than the others is what you're after, but you're going to need to do much better entropy analysis to find out anything more useful than a quick visual representation. 您所追求的是连续的线条,其中没有一条比其他线条更密集,但是您将需要进行更好的熵分析,以找出比快速可视表示更有用的东西。 In your case, it looks like 6 is a little more favored. 在您的情况下,看起来6更受欢迎。 Also, it looks like it repeats fairly often. 同样,它看起来很重复。

在此处输入图片说明

I would prefer histogram plot to check for uniformity of distribution of your random number generator. 我希望直方图可以检查随机数生成器的分布均匀性。

import numpy as np
import matplotlib
import matplotlib.pyplot as plt 
myarr = np.random.randint(1000, size=100000)
plt.hist(myarr, bins=40)
plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM