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). 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?
I would try random.rand and 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:
However, I'm getting an IndexError
when I go to higher ranges. 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. 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. Also, it looks like it repeats fairly often.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.