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.
-
3Do you need e, or do you need a reproducible sequence of random digits? They're different questions. – Thomas Feb 04 '12 at 20:18
-
Out of curiosity, what is the pseudorandom algorithm based on digits of *e*? There are a few well known approaches, from middle-square to Blum-Blum-Snub to Mersienne twister, that are fast and good enough. But none of these is based on irrational representations. – kkm inactive - support strike Feb 05 '12 at 04:54
4 Answers
Yes! I did it with continued fraction!
I found these code from Generating digits of square root of 2
def z(contfrac, a=1, b=0, c=0, d=1):
for x in contfrac:
while a > 0 and b > 0 and c > 0 and d > 0:
t = a // c
t2 = b // d
if not t == t2:
break
yield t
a = (10 * (a - c*t))
b = (10 * (b - d*t))
# continue with same fraction, don't pull new x
a, b = x*a+b, a
c, d = x*c+d, c
for digit in rdigits(a, c):
yield digit
def rdigits(p, q):
while p > 0:
if p > q:
d = p // q
p = p - q * d
else:
d = (10 * p) // q
p = 10 * p - q * d
yield d
I made the continued fraction generator:
def e_cf_expansion():
yield 1
k = 0
while True:
yield k
k += 2
yield 1
yield 1
and put them together:
def e_dec():
return z(e_cf_expansion())
Then:
>>> gen = e_dec()
>>> e = [str(gen.next()) for i in xrange(1000)]
>>> e.insert(1, '.')
>>> print ''.join(e)
2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035
Bonus: Code to generate continued fraction for sqrt(n) where n is a positive integer and sqrt(n) is irrational:
def sqrt_cf_expansion(S):
"""Generalized generator to compute continued
fraction representation of sqrt(S)"""
m = 0
d = 1
a = int(math.sqrt(S))
a0 = a
while True:
yield a
m = d*a-m
d = (S-m**2)//d
a = (a0+m)//d
If you call random.seed(n)
from the random
module with a known n
, the result will be the same each time:
>>> import random
>>> random.seed(4) # chosen by fair dice roll
>>> random.randint(0, 9)
2
>>> random.randint(0, 9)
1
>>> random.randint(0, 9)
3
>>> random.randint(0, 9)
1
>>> random.seed(4) # same seed as above
>>> random.randint(0, 9)
2
>>> random.randint(0, 9)
1
>>> random.randint(0, 9)
3
>>> random.randint(0, 9)
1
If you need to pass the state around, use the Random
class (somewhat underdocumented):
>>> r = random.Random(4)
>>> r.randint(0, 9)
2
>>> r.randint(0, 9)
1
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:
def random_digits(seed):
r = random.Random(seed)
while True:
yield r.randint(0, 9)

- 174,939
- 50
- 355
- 478
-
This will yield a rational number, as a deterministic PRNG always has cyclic behaviour. – May 27 '14 at 11:26
-
Hmm, good point, though largely theoretical because their period is far too large for any computer to hold the entire cycle in memory. – Thomas May 27 '14 at 15:50
Are you maybe looking for something like this:
>>> import math
>>> i = 1
>>> while i < 10:
... print('e = {0:.{1}f}'.format(math.e, i))
... i += 1
...
e = 2.7
e = 2.72
e = 2.718
e = 2.7183
e = 2.71828
e = 2.718282
e = 2.7182818
e = 2.71828183
e = 2.718281828
The standard library can give you math.e
: the mathematical constant e = 2.718281..., to available precision.

- 28,332
- 6
- 65
- 82
-
-
@samboosalis: Excellet observation, but I think I did it so it could've been easily changed into `while True` to generate all of them. – Rik Poggi Dec 02 '13 at 17:17
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.

- 708
- 4
- 11