本文整理汇总了Python中sage.misc.prandom.randint函数的典型用法代码示例。如果您正苦于以下问题:Python randint函数的具体用法?Python randint怎么用?Python randint使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了randint函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: random_element
def random_element(self):
r"""
Return a random element in self.
EXAMPLES::
sage: F = FreeGroup(3)
sage: F.subset(3).random_element() # random
aBa
"""
if self._n == 0:
return self._free_group.one()
alphabet = self._free_group.alphabet().list()
D = len(alphabet)
d = D/2
from sage.misc.prandom import randint
j = randint(0,D-1)
data = [alphabet[j]]
while len(data) != self._n:
if j < d:
i = j + d
else:
i = j - d
j = randint(0,D-2)
if j >= i:
j += 1
data.append(alphabet[j])
return self(data, check=False)
示例2: random_chain_complex
def random_chain_complex(level=1):
"""
Return a random chain complex, defined by specifying a single
random matrix in a random degree, with differential of degree
either 1 or -1. The matrix is randomly sparse or dense.
:param level: measure of complexity: the larger this is, the
larger the matrix can be, and the larger its degree can be in
the chain complex.
:type level: positive integer; optional, default 1
EXAMPLES::
sage: from sage.homology.tests import random_chain_complex
sage: C = random_chain_complex()
sage: C
Chain complex with at most 2 nonzero terms over Integer Ring
sage: C.degree_of_differential() # random: either 1 or -1
1
"""
bound = 50*level
nrows = randint(0, bound)
ncols = randint(0, bound)
sparseness = bool(randint(0, 1))
mat = random_matrix(ZZ, nrows, ncols, sparse=sparseness)
dim = randint(-bound, bound)
deg = 2 * randint(0, 1) - 1 # -1 or 1
return ChainComplex({dim: mat}, degree = deg)
示例3: random_simplicial_complex
def random_simplicial_complex(level=1, p=0.5):
"""
Return a random simplicial complex.
:param level: measure of complexity: the larger this is, the more
vertices and therefore the larger the possible dimension of the
complex.
:type level: positive integer; optional, default 1
:param p: probability, passed on to ``simplicial_complexes.RandomComplex``
:type p: float between 0 and 1; optional; default 0.5
EXAMPLES::
sage: from sage.homology.tests import random_simplicial_complex
sage: X = random_simplicial_complex()
sage: X # random
Simplicial complex with vertex set (0, 1, 2, 3, 4, 5, 6, 7) and 31 facets
sage: X.dimension() < 11
True
"""
from sage.misc.prandom import randint
from sage.homology.examples import simplicial_complexes
n = randint(2, 4*level)
dim = randint(1, n)
return simplicial_complexes.RandomComplex(n, dim, p)
示例4: transmit_unsafe
def transmit_unsafe(self, message):
r"""
Returns ``message`` with as many errors as ``self._number_errors`` in it, and as many erasures
as ``self._number_erasures`` in it.
If ``self._number_errors`` was passed as an tuple for the number of errors, it will
pick a random integer between the bounds of the tuple and use it as the number of errors.
It does the same with ``self._number_erasures``.
All erased positions are set to 0 in the transmitted message.
It is guaranteed that the erasures and the errors will never overlap:
the received message will always contains exactly as many errors and erasures
as expected.
This method does not check if ``message`` belongs to the input space of``self``.
INPUT:
- ``message`` -- a vector
OUTPUT:
- a couple of vectors, namely:
- the transmitted message, which is ``message`` with erroneous and erased positions
- the erasure vector, which contains ``1`` at the erased positions of the transmitted message
, 0 elsewhere.
EXAMPLES::
sage: F = GF(59)^11
sage: n_err, n_era = 2, 2
sage: Chan = channels.ErrorErasureChannel(F, n_err, n_era)
sage: msg = F((3, 14, 15, 9, 26, 53, 58, 9, 7, 9, 3))
sage: set_random_seed(10)
sage: Chan.transmit_unsafe(msg)
((31, 0, 15, 9, 38, 53, 58, 9, 0, 9, 3), (0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0))
"""
number_errors = randint(*self.number_errors())
number_erasures = randint(*self.number_erasures())
V = self.input_space()
n = V.dimension()
zero = V.base_ring().zero()
errors = sample(xrange(n), number_errors + number_erasures)
error_positions = errors[:number_errors]
erasure_positions = errors[number_errors:]
error_vector = random_error_vector(n, V.base_ring(), error_positions)
erasure_vector = random_error_vector(n , GF(2), erasure_positions)
message = message + error_vector
for i in erasure_positions:
message[i] = zero
return message, erasure_vector
示例5: random_even_arithgroup
def random_even_arithgroup(index,nu2_max=None,nu3_max=None):
r"""
Return a random even arithmetic subgroup
EXAMPLES::
sage: import sage.modular.arithgroup.tests as tests
sage: G = tests.random_even_arithgroup(30); G # random
Arithmetic subgroup of index 30
sage: G.is_even()
True
"""
from sage.groups.perm_gps.permgroup import PermutationGroup
test = False
if nu2_max is None:
nu2_max = index//5
elif nu2_max == 0:
assert index%2 == 0
if nu3_max is None:
nu3_max = index//7
elif nu3_max == 0:
assert index%3 == 0
while not test:
nu2 = prandom.randint(0,nu2_max)
nu2 = index%2 + nu2*2
nu3 = prandom.randint(0,nu3_max)
nu3 = index%3 + nu3*3
l = range(1,index+1)
prandom.shuffle(l)
S2 = []
for i in xrange(nu2):
S2.append((l[i],))
for i in xrange(nu2,index,2):
S2.append((l[i],l[i+1]))
prandom.shuffle(l)
S3 = []
for i in xrange(nu3):
S3.append((l[i],))
for i in xrange(nu3,index,3):
S3.append((l[i],l[i+1],l[i+2]))
G = PermutationGroup([S2,S3])
test = G.is_transitive()
return ArithmeticSubgroup_Permutation(S2=S2,S3=S3)
示例6: random_lattice
def random_lattice(): # Random Lattice
from sage.misc.prandom import randint
n = get_stats().counts()['nlattice']
n = randint(0,n-1)
C = getDBConnection()
res = C.Lattices.lat.find()[n]
return redirect(url_for(".render_lattice_webpage", label=res['label']))
示例7: _random_element_from_unrank
def _random_element_from_unrank(self):
"""
A random element in ``self``.
``self.random_element()`` returns a random element in
``self`` with uniform probability.
This is the default implementation from the category
``EnumeratedSet()`` which uses the method ``unrank``.
EXAMPLES::
sage: C = FiniteEnumeratedSets().example()
sage: C.random_element()
1
sage: C._random_element_from_unrank()
2
TODO: implement _test_random which checks uniformness
"""
from sage.misc.prandom import randint
c = self.cardinality()
r = randint(0, c - 1)
return self.unrank(r)
示例8: random_letter
def random_letter(self, exclude=[]):
"""
A random letter, different from the letters in ``exclude``.
INPUT:
- ``exclude`` -- (default:[]) list of letter to exclude
OUTPUT:
- return a random letter different from letter in exclude
EXAMPLES::
sage: A = AlphabetWithInverses(['a','b','c'], ['A','B','C'])
sage: A.random_letter(['a','b','c','A','C'])
'B'
"""
from sage.misc.prandom import randint
done = False
while not done:
j = randint(0, 2 * len(self) - 1)
a = self[j]
done = a not in exclude
return a
示例9: random_element
def random_element(self):
"""
Return a random element of this dual group.
EXAMPLES::
sage: G = AbelianGroup([2,3,9])
sage: Gd = DualAbelianGroup(G)
sage: Gd.random_element()
X0*X1^2*X2
sage: N = 43^2-1
sage: G = AbelianGroup([N],names="a")
sage: Gd = DualAbelianGroup(G,names="A")
sage: a, = G.gens()
sage: A, = Gd.gens()
sage: x = a^(N/4); y = a^(N/3); z = a^(N/14)
sage: X = Gd.random_element(); X
A^615
sage: len([a for a in [x,y,z] if abs(X(a)-1)>10^(-8)])
2
"""
from sage.misc.prandom import randint
gens = self.gens()
g = gens[0]**0
for i in range(len(gens)):
g = g*gens[i]**(randint(1,gens[i].order()))
return g
示例10: random_hmf
def random_hmf(): # Random Hilbert modular form
from sage.misc.prandom import randint
n = get_stats().counts()['nforms']
n = randint(0,n-1)
C = getDBConnection()
res = C.hmfs.forms.find()[n]
return redirect(url_for(".render_hmf_webpage", field_label=res['field_label'], label=res['label']))
示例11: random_element
def random_element(self):
r"""
Return a random parking function of size `n`.
The algorithm uses a circular parking space with `n+1`
spots. Then all `n` cars can park and there remains one empty
spot. Spots are then renumbered so that the empty spot is `0`.
The probability distribution is uniform on the set of
`(n+1)^{n-1}` parking functions of size `n`.
EXAMPLES::
sage: pf = ParkingFunctions(8)
sage: a = pf.random_element(); a # random
[5, 7, 2, 4, 2, 5, 1, 3]
sage: a in pf
True
"""
n = self.n
Zm = Zmod(n + 1)
fun = [Zm(randint(0, n)) for i in range(n)]
free = [Zm(j) for j in range(n + 1)]
for car in fun:
position = car
while not(position in free):
position += Zm.one()
free.remove(position)
return ParkingFunction([(i - free[0]).lift() for i in fun])
示例12: random_element
def random_element(self, bound=None):
"""
Return a random element of this ring.
INPUT:
- ``bound``, a positive integer or ``None`` (the default). Is given,
return the coercion of an integer in the interval
``[-bound, bound]`` into this ring.
EXAMPLES::
sage: R = IntegerModRing(18)
sage: R.random_element()
2
We test ``bound``-option::
sage: R.random_element(2) in [R(16), R(17), R(0), R(1), R(2)]
True
"""
if not (bound is None):
return commutative_ring.CommutativeRing.random_element(self, bound)
a = random.randint(0, self.order() - 1)
return self(a)
示例13: _rand_der
def _rand_der(self):
"""
Produces a random derangement of `[1, 2, \ldots, n]`.
This is an
implementation of the algorithm described by Martinez et. al. in
[Martinez08]_.
EXAMPLES::
sage: D = Derangements(4)
sage: D._rand_der()
[2, 3, 4, 1]
"""
n = len(self._set)
A = list(range(1, n + 1))
mark = [x<0 for x in A]
i,u = n,n
while u >= 2:
if not(mark[i-1]):
while True:
j = randint(1,i-1)
if not(mark[j-1]):
A[i-1], A[j-1] = A[j-1], A[i-1]
break
p = random()
if p < (u-1) * self._count_der(u-2) // self._count_der(u):
mark[j-1] = True
u -= 1
u -= 1
i -= 1
return A
示例14: random_hmf
def random_hmf(): # Random Hilbert modular form
from sage.misc.prandom import randint
n = get_stats().counts()['nforms']
n = randint(0,n-1)
C = getDBConnection()
res = C.hmfs.forms.find()[n]
return hilbert_modular_form_by_label(res)
示例15: random_element
def random_element(self):
"""
Return a random element of this dual group.
EXAMPLES::
sage: G = AbelianGroup([2,3,9])
sage: Gd = G.dual_group(base_ring=CC)
sage: Gd.random_element()
X1^2
sage: N = 43^2-1
sage: G = AbelianGroup([N],names="a")
sage: Gd = G.dual_group(names="A", base_ring=CC)
sage: a, = G.gens()
sage: A, = Gd.gens()
sage: x = a^(N/4); y = a^(N/3); z = a^(N/14)
sage: X = A*Gd.random_element(); X
A^615
sage: len([a for a in [x,y,z] if abs(X(a)-1)>10^(-8)])
2
"""
from sage.misc.prandom import randint
result = self.one()
for g in self.gens():
order = g.order()
result *= g**(randint(0,order))
return result