sympy: what's wrong with this picture?

Mensanator mensanator at aol.com
Tue Mar 4 15:13:44 EST 2008


On Mar 4, 12:32 pm, Nanjundi <nanju... at gmail.com> wrote:
> On Mar 3, 3:40 pm, Mensanator <mensana... at aol.com> wrote:
>
>
>
>
>
> > Notice anything funny about the "random" choices?
>
> > import sympy
> > 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
> >   ff = sympy.factorint(C)
> >   print ff
>
> > ##  7307 7243 [(7243, 1), (7307, 1)]
> > ##  4091 6829 [(4091, 1), (6829, 1)]
> > ##  8563 2677 [(2677, 1), (8563, 1)]
> > ##  4091 6829 [(4091, 1), (6829, 1)]
> > ##  8563 2677 [(2677, 1), (8563, 1)]
> > ##  4091 6829 [(4091, 1), (6829, 1)]
> > ##  8563 2677 [(2677, 1), (8563, 1)]
> > ##  4091 6829 [(4091, 1), (6829, 1)]
> > ##  8563 2677 [(2677, 1), (8563, 1)]
> > ##  4091 6829 [(4091, 1), (6829, 1)]
>
> > As in, "they're NOT random".
>
> > The random number generator is broken by the sympy.factorint()
> > function.
>
> > Random.choice() works ok if the factorint() function commented out.
>
> > ##  6089 1811 None
> > ##  6449 1759 None
> > ##  9923 4639 None
> > ##  4013 4889 None
> > ##  4349 2029 None
> > ##  6703 8677 None
> > ##  1879 1867 None
> > ##  5153 5279 None
> > ##  2011 4937 None
> > ##  7253 5507 None
>
> > This makes sympy worse than worthless, as it fucks up other modules.
>
> 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.

Frankly, I don't understand why factorint() reseeds at all.
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.

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

> -N




More information about the Python-list mailing list