PolynomialRing().random_element() returns 0 rather often.

83 views
Skip to first unread message

Georgi Guninski

unread,
Dec 30, 2023, 7:43:44 AM12/30/23
to sage-...@googlegroups.com
Just FYI:

def testquotient2():
set_random_seed(1);p=next_prime(10**120);Kx=Integers(p)['x']
l=[Kx.random_element() for _ in range(100)]
return l.count(0)
testquotient2()
27

Nils Bruin

unread,
Dec 30, 2023, 10:38:54 AM12/30/23
to sage-devel
This is documented (almost). The routine generates polynomials with degrees in a given range, with by default is -1..2 . It chooses the degree uniformly (the documentation doesn't specify this), so one would expect 33% degree -1, i.e., the 0 poly. I have a hard time thinking of situations where this is the distribution one would want.

Over a finite field perhaps you're more interested in the uniform distribution on polynomials of degree at most N. Then Kx([GF(p).random_element() for _ in range(N+1)]) would be better.

Maxime Bombar

unread,
Dec 30, 2023, 12:45:46 PM12/30/23
to sage-...@googlegroups.com, Georgi Guninski
Hi,

this is because PolynomialRing().random_element() takes an optional
argument degree which should either be an integer greater or equal to
-1, or an ordered pair of such integers which defines an integer range
in which the degree is selected uniformly at random. By convention,
degree -1 yields the 0 polynomial, degree 0 yields a constant non zero
and so on.

By default, this argument is set to (-1, 2), which means that which 25%
probability, degree -1 will be selected, and this is exactly what you
observe with your experiment. But you can set degree to whatever suits
you better, e.g. starting with 0 instead of -1 to avoid the 0 polynomial.

For more information:

https://doc.sagemath.org/html/en/reference/polynomial_rings/sage/rings/polynomial/polynomial_ring.html#sage.rings.polynomial.polynomial_ring.PolynomialRing_general.random_element


Best,

--
Maxime

Reply all
Reply to author
Forward
0 new messages