On Mar 5, 9:29 am, Nanjundi <[EMAIL PROTECTED]> wrote:
> On Mar 4, 3:13 pm, Mensanator <[EMAIL PROTECTED]> wrote:
>
> > On Mar 4, 12:32 pm, Nanjundi <[EMAIL PROTECTED]> wrote:
>
> > > Does seeding ( random.seed ) random with time fix this? It should.
>
> > I suppose that depends on how long it takes factorint() to
> > process a number. If the seed is reset before the next clock
> > tick, you will get the same random numbers as the previous
> > iteration.
>
> Alright, then make it constant and don't worry about the clock tick.

Reseeding with a constant always sets the sequence to the same
starting
point.

>>> for i in xrange(10):
>
> ...   f1 = random.choice(f)
> ...   print f1,
> ...   f2 = random.choice(f)
> ...   print f2,
> ...   C = f1*f2
> ...   ff = None
> ...   ff = sympy.factorint(C)
> ...   print ff
> ...   random.seed(i)
> ...
> 5573 5171 [(5171, 1), (5573, 1)]
> 8537 7673 [(7673, 1), (8537, 1)]
> 2063 8573 [(2063, 1), (8573, 1)]
> 9551 9473 [(9473, 1), (9551, 1)]
> 2909 5659 [(2909, 1), (5659, 1)]
> 2897 1789 [(1789, 1), (2897, 1)]
> 6361 7541 [(6361, 1), (7541, 1)]
> 8017 8293 [(8017, 1), (8293, 1)]
> 3671 2207 [(2207, 1), (3671, 1)]
> 2803 9629 [(2803, 1), (9629, 1)]
>
> > Frankly, I don't understand why factorint() reseeds at all.
>
> Read the doc:
> *    The rho algorithm is a Monte Carlo method whose outcome can be
> affected by changing the random seed value.  *

But that doesn't give it the right to mess with the state
of the random number generator _I'm_ using. Had I actually
known what was happening, I could have saved the state of
my random number generator s=random.getstate() and then restored
it after calling factorint(), random.setstate(s).

import sympy   # with RK's patch removed
import time
import random

f = [i for i in sympy.primerange(1000,10000)]

for i in xrange(10):
  f1 = random.choice(f)
  print f1,
  f2 = random.choice(f)
  print f2,
  C = f1*f2
  ff = None
  rs = random.getstate()
  ff = sympy.factorint(C)
  random.setstate(rs)
  print ff

5669 3863 [(3863, 1), (5669, 1)]
1973 5431 [(1973, 1), (5431, 1)]
7577 6089 [(6089, 1), (7577, 1)]
8761 4957 [(4957, 1), (8761, 1)]
4153 2719 [(2719, 1), (4153, 1)]
4999 5669 [(4999, 1), (5669, 1)]
8863 5417 [(5417, 1), (8863, 1)]
7151 7951 [(7151, 1), (7951, 1)]
7867 9887 [(7867, 1), (9887, 1)]
9283 5227 [(5227, 1), (9283, 1)]

Of course, this is new as of Python 2.4, so if factorint()
tried to save & restore state, sympy wouldn't work on Python
2.3 or earlier.

If I'm reading RK's patch correctly, he doesn't reseed the
random number generator, he creates a new random object that
maintains it's own state that can be freely seeded to any
value without disturbing the state of my random number generator.

>
> > Doesn't Random automatically initialize the seed?
> > Doesn't constantly reseeding degrade the performance of the
> > random number generator? With Robert Kern's patch, the reseeding
> > is no longer a constant, fixing the immediate symptom.
>
> Does it matter?

I was wrong. It is a constant, just not 1234. If that's what
factorint() needs, fine. As long as it maintains a seperate state
than the one I'm using.

> The factorint reseeds using a constant seed (1234).

Not now it doesn't:

@@ -92,8 +92,8 @@ def pollard_pm1(n, B=10, seed=1234):

     """
     from math import log
-    random.seed(seed + B)
-    a = random.randint(2, n-1)
+    prng = random.Random(seed + B)
+    a = prng.randint(2, n-1)
     for p in sieve.primerange(2, B):
         e = int(log(B, p))
         a = pow(a, p**e, n)

>
> > But what if _I_ wanted to make a repeatable sequence for test
> > purposes? Wouldn't factorint() destroy my attempt by reseeding
> > on every call?
>
> Repeatable sequence? save it and reuse!

As part of my resolution to tone down my attitude,
I won't even reply to that.

> Think about "What if"s doesn't get any work done.

?

>
> -N

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to