Skip to content

Commit 1d9d490

Browse files
committed
moving random_element to category of rings
1 parent 766c7a0 commit 1d9d490

File tree

2 files changed

+25
-31
lines changed

2 files changed

+25
-31
lines changed

src/sage/categories/rings.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from sage.misc.cachefunc import cached_method
1818
from sage.misc.lazy_import import LazyImport
19+
from sage.misc.prandom import randint
1920
from sage.categories.category_with_axiom import CategoryWithAxiom
2021
from sage.categories.rngs import Rngs
2122
from sage.structure.element import Element
@@ -1646,6 +1647,30 @@ def _random_nonzero_element(self, *args, **kwds):
16461647
if not x.is_zero():
16471648
return x
16481649

1650+
def random_element(self, bound=2):
1651+
"""
1652+
Return a random integer coerced into this ring.
1653+
1654+
The integer is chosen uniformly
1655+
from the interval ``[-bound,bound]``.
1656+
1657+
INPUT:
1658+
1659+
- ``bound`` -- integer (default: 2)
1660+
1661+
ALGORITHM:
1662+
1663+
This uses Python's ``randint``.
1664+
1665+
EXAMPLES::
1666+
1667+
sage: ZZ.random_element(8) # random
1668+
1
1669+
sage: QQ.random_element(8) # random
1670+
2
1671+
"""
1672+
return randint(-bound, bound) * self.one()
1673+
16491674
class ElementMethods:
16501675
def is_unit(self) -> bool:
16511676
r"""

src/sage/rings/ring.pyx

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ from sage.misc.superseded import deprecation
114114
from sage.structure.coerce cimport coercion_model
115115
from sage.structure.parent cimport Parent
116116
from sage.structure.category_object cimport check_default_category
117-
from sage.misc.prandom import randint
118117
from sage.categories.rings import Rings
119118
from sage.categories.algebras import Algebras
120119
from sage.categories.commutative_algebras import CommutativeAlgebras
@@ -637,36 +636,6 @@ cdef class Ring(ParentWithGens):
637636
"""
638637
return self.zeta().multiplicative_order()
639638

640-
def random_element(self, bound=2):
641-
"""
642-
Return a random integer coerced into this ring, where the
643-
integer is chosen uniformly from the interval ``[-bound,bound]``.
644-
645-
INPUT:
646-
647-
- ``bound`` -- integer (default: 2)
648-
649-
ALGORITHM:
650-
651-
Uses Python's randint.
652-
653-
TESTS:
654-
655-
The following example returns a :exc:`NotImplementedError` since the
656-
generic ring class ``__call__`` function returns a
657-
:exc:`NotImplementedError`. Note that
658-
``sage.rings.ring.Ring.random_element`` performs a call in the generic
659-
ring class by a random integer::
660-
661-
sage: R = sage.rings.ring.Ring(ZZ); R
662-
<sage.rings.ring.Ring object at ...>
663-
sage: R.random_element()
664-
Traceback (most recent call last):
665-
...
666-
NotImplementedError: cannot construct elements of <sage.rings.ring.Ring object at ...>
667-
"""
668-
return self(randint(-bound,bound))
669-
670639
@cached_method
671640
def epsilon(self):
672641
"""

0 commit comments

Comments
 (0)