Ask Your Question

Revision history [back]

This is because randint returns a Python int and not a Sage Integer, and the division of two Python ints leads to an int:

sage: type(randint(2,10))
<type 'int'>

sage: int(5) / int(3)

When you write 1/a, since the 1 is a Sage integer, then the coercion makes the division happen in the set od Sage integers, this explains why you got a rational number:

sage: type(1)
<type 'sage.rings.integer.Integer'>

sage: get_coercion_model().common_parent(1,int(3))
Integer Ring

If you want to get random Sage integers, you can either convert the int into Sage integers:

sage: a = ZZ(randint(2,10))
sage: a
sage: type(a)
<type 'sage.rings.integer.Integer'>

Or, you can ask Sage to produce a random integer:

sage: a = ZZ.random_element(2,11)
sage: a
sage: type(a)
<type 'sage.rings.integer.Integer'>

Note that i replaced the 10 by 11, since in the random_element of Sage, the right bound is excluded, while it is not with the randint function.