Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Generate random rationals in Sage """
from sage.libs.gmp.mpz cimport * from sage.libs.gmp.mpq cimport * from sage.misc.randstate cimport randstate, current_randstate, SAGE_RAND_MAX
###########################
cdef inline void mpq_randomize_entry(mpq_t x, mpz_t num_bound, mpz_t den_bound):
cdef inline void mpq_randomize_entry_nonzero(mpq_t x, mpz_t num_bound, mpz_t den_bound):
cdef inline void mpq_randomize_entry_as_int(mpq_t x, mpz_t bound): cdef randstate rstate = current_randstate() mpz_urandomm(mpq_numref(x), rstate.gmp_state, bound) mpz_set_si(mpq_denref(x), 1) if rstate.c_random() % 2: mpz_mul_si(mpq_numref(x), mpq_numref(x), -1)
cdef inline void mpq_randomize_entry_as_int_nonzero(mpq_t x, mpz_t bound): mpq_randomize_entry_as_int(x, bound) while mpq_sgn(x) == 0: mpq_randomize_entry_as_int(x, bound)
cdef inline void mpq_randomize_entry_recip_uniform(mpq_t x): # Numerator is selected the same way as ZZ.random_element(); # denominator is selected in a similar way, but # modified to give only positive integers. (The corresponding # probability distribution is $X = \mbox{trunc}(1/R)$, where R # varies uniformly between 0 and 1.)
cdef inline void mpq_randomize_entry_recip_uniform_nonzero(mpq_t x): mpq_randomize_entry_recip_uniform_nonzero(x) while mpq_sgn(x) == 0: mpq_randomize_entry_recip_uniform_nonzero(x) |