I am trying to make a constant random number generators (I mean a RNG that outputs a series of numbers that doesn't repeat, but stays the same every time it starts from the beginning). I have one for pi. I need an algorithm to generate e digit by digit to feed into the RNG, preferably in form of Python iterator or generator. I also welcome codes that generates other irrational numbers. Thanks in advance.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Yes! I did it with continued fraction!
I found these code from Generating digits of square root of 2
I made the continued fraction generator:
and put them together:
Then:
Bonus: Code to generate continued fraction for sqrt(n) where n is a positive integer and sqrt(n) is irrational:
If you call
random.seed(n)
from therandom
module with a knownn
, the result will be the same each time:If you need to pass the state around, use the
Random
class (somewhat underdocumented):It's easy to make a generator out of this which lets you produce multiple sequences that don't step on each other's toes:
Are you maybe looking for something like this:
The standard library can give you
math.e
: the mathematical constant e = 2.718281..., to available precision.If you are willing to use pi instead of e there is a digit extraction algorithm for pi. It is unknown if such an algorithm exists for e. The file bbp_pi.py in sympy provides a fine implementation of the algorithm.