passagemath-objects 10.6.41__cp314-cp314t-macosx_13_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of passagemath-objects might be problematic. Click here for more details.
- passagemath_objects/.dylibs/libgmp.10.dylib +0 -0
- passagemath_objects/__init__.py +3 -0
- passagemath_objects-10.6.41.dist-info/METADATA +115 -0
- passagemath_objects-10.6.41.dist-info/RECORD +280 -0
- passagemath_objects-10.6.41.dist-info/WHEEL +6 -0
- passagemath_objects-10.6.41.dist-info/top_level.txt +3 -0
- sage/all__sagemath_objects.py +37 -0
- sage/arith/all__sagemath_objects.py +5 -0
- sage/arith/long.pxd +411 -0
- sage/arith/numerical_approx.cpython-314t-darwin.so +0 -0
- sage/arith/numerical_approx.pxd +35 -0
- sage/arith/numerical_approx.pyx +75 -0
- sage/arith/power.cpython-314t-darwin.so +0 -0
- sage/arith/power.pxd +31 -0
- sage/arith/power.pyx +127 -0
- sage/categories/action.cpython-314t-darwin.so +0 -0
- sage/categories/action.pxd +29 -0
- sage/categories/action.pyx +641 -0
- sage/categories/algebra_functor.py +745 -0
- sage/categories/all__sagemath_objects.py +33 -0
- sage/categories/basic.py +62 -0
- sage/categories/cartesian_product.py +295 -0
- sage/categories/category.py +3401 -0
- sage/categories/category_cy_helper.cpython-314t-darwin.so +0 -0
- sage/categories/category_cy_helper.pxd +8 -0
- sage/categories/category_cy_helper.pyx +322 -0
- sage/categories/category_singleton.cpython-314t-darwin.so +0 -0
- sage/categories/category_singleton.pxd +3 -0
- sage/categories/category_singleton.pyx +342 -0
- sage/categories/category_types.py +637 -0
- sage/categories/category_with_axiom.py +2876 -0
- sage/categories/covariant_functorial_construction.py +703 -0
- sage/categories/facade_sets.py +228 -0
- sage/categories/functor.cpython-314t-darwin.so +0 -0
- sage/categories/functor.pxd +7 -0
- sage/categories/functor.pyx +691 -0
- sage/categories/homset.py +1338 -0
- sage/categories/homsets.py +364 -0
- sage/categories/isomorphic_objects.py +73 -0
- sage/categories/map.cpython-314t-darwin.so +0 -0
- sage/categories/map.pxd +34 -0
- sage/categories/map.pyx +2112 -0
- sage/categories/morphism.cpython-314t-darwin.so +0 -0
- sage/categories/morphism.pxd +14 -0
- sage/categories/morphism.pyx +895 -0
- sage/categories/objects.py +167 -0
- sage/categories/primer.py +1696 -0
- sage/categories/pushout.py +4834 -0
- sage/categories/quotients.py +64 -0
- sage/categories/realizations.py +200 -0
- sage/categories/sets_cat.py +3228 -0
- sage/categories/sets_with_partial_maps.py +52 -0
- sage/categories/subobjects.py +64 -0
- sage/categories/subquotients.py +21 -0
- sage/categories/with_realizations.py +311 -0
- sage/cpython/__init__.py +19 -0
- sage/cpython/_py2_random.py +619 -0
- sage/cpython/all.py +3 -0
- sage/cpython/atexit.cpython-314t-darwin.so +0 -0
- sage/cpython/atexit.pyx +269 -0
- sage/cpython/builtin_types.cpython-314t-darwin.so +0 -0
- sage/cpython/builtin_types.pyx +7 -0
- sage/cpython/cython_metaclass.cpython-314t-darwin.so +0 -0
- sage/cpython/cython_metaclass.h +117 -0
- sage/cpython/cython_metaclass.pxd +3 -0
- sage/cpython/cython_metaclass.pyx +130 -0
- sage/cpython/debug.cpython-314t-darwin.so +0 -0
- sage/cpython/debug.pyx +302 -0
- sage/cpython/dict_del_by_value.cpython-314t-darwin.so +0 -0
- sage/cpython/dict_del_by_value.pxd +9 -0
- sage/cpython/dict_del_by_value.pyx +191 -0
- sage/cpython/dict_internal.h +245 -0
- sage/cpython/getattr.cpython-314t-darwin.so +0 -0
- sage/cpython/getattr.pxd +9 -0
- sage/cpython/getattr.pyx +439 -0
- sage/cpython/pycore_long.h +97 -0
- sage/cpython/pycore_long.pxd +10 -0
- sage/cpython/python_debug.h +44 -0
- sage/cpython/python_debug.pxd +47 -0
- sage/cpython/pyx_visit.h +13 -0
- sage/cpython/string.cpython-314t-darwin.so +0 -0
- sage/cpython/string.pxd +76 -0
- sage/cpython/string.pyx +34 -0
- sage/cpython/string_impl.h +60 -0
- sage/cpython/type.cpython-314t-darwin.so +0 -0
- sage/cpython/type.pxd +2 -0
- sage/cpython/type.pyx +40 -0
- sage/cpython/wrapperdescr.pxd +67 -0
- sage/ext/all__sagemath_objects.py +3 -0
- sage/ext/ccobject.h +64 -0
- sage/ext/cplusplus.pxd +17 -0
- sage/ext/mod_int.h +30 -0
- sage/ext/mod_int.pxd +24 -0
- sage/ext/stdsage.pxd +39 -0
- sage/groups/all__sagemath_objects.py +1 -0
- sage/groups/group.cpython-314t-darwin.so +0 -0
- sage/groups/group.pxd +14 -0
- sage/groups/group.pyx +322 -0
- sage/groups/old.cpython-314t-darwin.so +0 -0
- sage/groups/old.pxd +14 -0
- sage/groups/old.pyx +219 -0
- sage/libs/all__sagemath_objects.py +3 -0
- sage/libs/gmp/__init__.py +1 -0
- sage/libs/gmp/all.pxd +6 -0
- sage/libs/gmp/binop.pxd +23 -0
- sage/libs/gmp/misc.pxd +8 -0
- sage/libs/gmp/mpf.pxd +88 -0
- sage/libs/gmp/mpn.pxd +57 -0
- sage/libs/gmp/mpq.pxd +57 -0
- sage/libs/gmp/mpz.pxd +202 -0
- sage/libs/gmp/pylong.cpython-314t-darwin.so +0 -0
- sage/libs/gmp/pylong.pxd +12 -0
- sage/libs/gmp/pylong.pyx +150 -0
- sage/libs/gmp/random.pxd +25 -0
- sage/libs/gmp/randomize.pxd +59 -0
- sage/libs/gmp/types.pxd +53 -0
- sage/libs/gmpxx.pxd +19 -0
- sage/misc/abstract_method.py +276 -0
- sage/misc/all__sagemath_objects.py +43 -0
- sage/misc/bindable_class.py +253 -0
- sage/misc/c3_controlled.cpython-314t-darwin.so +0 -0
- sage/misc/c3_controlled.pxd +2 -0
- sage/misc/c3_controlled.pyx +1402 -0
- sage/misc/cachefunc.cpython-314t-darwin.so +0 -0
- sage/misc/cachefunc.pxd +43 -0
- sage/misc/cachefunc.pyx +3781 -0
- sage/misc/call.py +188 -0
- sage/misc/classcall_metaclass.cpython-314t-darwin.so +0 -0
- sage/misc/classcall_metaclass.pxd +14 -0
- sage/misc/classcall_metaclass.pyx +599 -0
- sage/misc/constant_function.cpython-314t-darwin.so +0 -0
- sage/misc/constant_function.pyx +130 -0
- sage/misc/decorators.py +747 -0
- sage/misc/fast_methods.cpython-314t-darwin.so +0 -0
- sage/misc/fast_methods.pxd +20 -0
- sage/misc/fast_methods.pyx +351 -0
- sage/misc/flatten.py +90 -0
- sage/misc/fpickle.cpython-314t-darwin.so +0 -0
- sage/misc/fpickle.pyx +177 -0
- sage/misc/function_mangling.cpython-314t-darwin.so +0 -0
- sage/misc/function_mangling.pxd +11 -0
- sage/misc/function_mangling.pyx +308 -0
- sage/misc/inherit_comparison.cpython-314t-darwin.so +0 -0
- sage/misc/inherit_comparison.pxd +5 -0
- sage/misc/inherit_comparison.pyx +105 -0
- sage/misc/instancedoc.cpython-314t-darwin.so +0 -0
- sage/misc/instancedoc.pyx +331 -0
- sage/misc/lazy_attribute.cpython-314t-darwin.so +0 -0
- sage/misc/lazy_attribute.pyx +607 -0
- sage/misc/lazy_format.py +135 -0
- sage/misc/lazy_import.cpython-314t-darwin.so +0 -0
- sage/misc/lazy_import.pyx +1299 -0
- sage/misc/lazy_import_cache.py +36 -0
- sage/misc/lazy_list.cpython-314t-darwin.so +0 -0
- sage/misc/lazy_list.pxd +19 -0
- sage/misc/lazy_list.pyx +1187 -0
- sage/misc/lazy_string.cpython-314t-darwin.so +0 -0
- sage/misc/lazy_string.pxd +7 -0
- sage/misc/lazy_string.pyx +546 -0
- sage/misc/misc.py +1066 -0
- sage/misc/misc_c.cpython-314t-darwin.so +0 -0
- sage/misc/misc_c.pxd +3 -0
- sage/misc/misc_c.pyx +766 -0
- sage/misc/namespace_package.py +37 -0
- sage/misc/nested_class.cpython-314t-darwin.so +0 -0
- sage/misc/nested_class.pxd +3 -0
- sage/misc/nested_class.pyx +394 -0
- sage/misc/persist.cpython-314t-darwin.so +0 -0
- sage/misc/persist.pyx +1251 -0
- sage/misc/prandom.py +418 -0
- sage/misc/randstate.cpython-314t-darwin.so +0 -0
- sage/misc/randstate.pxd +30 -0
- sage/misc/randstate.pyx +1059 -0
- sage/misc/repr.py +203 -0
- sage/misc/reset.cpython-314t-darwin.so +0 -0
- sage/misc/reset.pyx +196 -0
- sage/misc/sage_ostools.cpython-314t-darwin.so +0 -0
- sage/misc/sage_ostools.pyx +323 -0
- sage/misc/sage_timeit.py +276 -0
- sage/misc/sage_timeit_class.cpython-314t-darwin.so +0 -0
- sage/misc/sage_timeit_class.pyx +120 -0
- sage/misc/sage_unittest.py +637 -0
- sage/misc/sageinspect.py +2768 -0
- sage/misc/session.cpython-314t-darwin.so +0 -0
- sage/misc/session.pyx +392 -0
- sage/misc/superseded.py +557 -0
- sage/misc/test_nested_class.py +228 -0
- sage/misc/timing.py +264 -0
- sage/misc/unknown.py +222 -0
- sage/misc/verbose.py +253 -0
- sage/misc/weak_dict.cpython-314t-darwin.so +0 -0
- sage/misc/weak_dict.pxd +15 -0
- sage/misc/weak_dict.pyx +1231 -0
- sage/modules/all__sagemath_objects.py +1 -0
- sage/modules/module.cpython-314t-darwin.so +0 -0
- sage/modules/module.pxd +5 -0
- sage/modules/module.pyx +329 -0
- sage/rings/all__sagemath_objects.py +3 -0
- sage/rings/integer_fake.h +22 -0
- sage/rings/integer_fake.pxd +55 -0
- sage/sets/all__sagemath_objects.py +3 -0
- sage/sets/pythonclass.cpython-314t-darwin.so +0 -0
- sage/sets/pythonclass.pxd +9 -0
- sage/sets/pythonclass.pyx +247 -0
- sage/structure/__init__.py +4 -0
- sage/structure/all.py +30 -0
- sage/structure/category_object.cpython-314t-darwin.so +0 -0
- sage/structure/category_object.pxd +28 -0
- sage/structure/category_object.pyx +1087 -0
- sage/structure/coerce.cpython-314t-darwin.so +0 -0
- sage/structure/coerce.pxd +44 -0
- sage/structure/coerce.pyx +2107 -0
- sage/structure/coerce_actions.cpython-314t-darwin.so +0 -0
- sage/structure/coerce_actions.pxd +27 -0
- sage/structure/coerce_actions.pyx +988 -0
- sage/structure/coerce_dict.cpython-314t-darwin.so +0 -0
- sage/structure/coerce_dict.pxd +51 -0
- sage/structure/coerce_dict.pyx +1557 -0
- sage/structure/coerce_exceptions.py +23 -0
- sage/structure/coerce_maps.cpython-314t-darwin.so +0 -0
- sage/structure/coerce_maps.pxd +28 -0
- sage/structure/coerce_maps.pyx +718 -0
- sage/structure/debug_options.cpython-314t-darwin.so +0 -0
- sage/structure/debug_options.pxd +6 -0
- sage/structure/debug_options.pyx +54 -0
- sage/structure/dynamic_class.py +541 -0
- sage/structure/element.cpython-314t-darwin.so +0 -0
- sage/structure/element.pxd +272 -0
- sage/structure/element.pyx +4772 -0
- sage/structure/element_wrapper.cpython-314t-darwin.so +0 -0
- sage/structure/element_wrapper.pxd +12 -0
- sage/structure/element_wrapper.pyx +582 -0
- sage/structure/factorization.py +1422 -0
- sage/structure/factorization_integer.py +105 -0
- sage/structure/factory.cpython-314t-darwin.so +0 -0
- sage/structure/factory.pyx +786 -0
- sage/structure/formal_sum.py +489 -0
- sage/structure/gens_py.py +73 -0
- sage/structure/global_options.py +1743 -0
- sage/structure/indexed_generators.py +863 -0
- sage/structure/list_clone.cpython-314t-darwin.so +0 -0
- sage/structure/list_clone.pxd +65 -0
- sage/structure/list_clone.pyx +1867 -0
- sage/structure/list_clone_demo.cpython-314t-darwin.so +0 -0
- sage/structure/list_clone_demo.pyx +248 -0
- sage/structure/list_clone_timings.py +179 -0
- sage/structure/list_clone_timings_cy.cpython-314t-darwin.so +0 -0
- sage/structure/list_clone_timings_cy.pyx +86 -0
- sage/structure/mutability.cpython-314t-darwin.so +0 -0
- sage/structure/mutability.pxd +21 -0
- sage/structure/mutability.pyx +348 -0
- sage/structure/nonexact.py +69 -0
- sage/structure/parent.cpython-314t-darwin.so +0 -0
- sage/structure/parent.pxd +112 -0
- sage/structure/parent.pyx +3093 -0
- sage/structure/parent_base.cpython-314t-darwin.so +0 -0
- sage/structure/parent_base.pxd +13 -0
- sage/structure/parent_base.pyx +44 -0
- sage/structure/parent_gens.cpython-314t-darwin.so +0 -0
- sage/structure/parent_gens.pxd +22 -0
- sage/structure/parent_gens.pyx +377 -0
- sage/structure/parent_old.cpython-314t-darwin.so +0 -0
- sage/structure/parent_old.pxd +25 -0
- sage/structure/parent_old.pyx +294 -0
- sage/structure/proof/__init__.py +1 -0
- sage/structure/proof/all.py +243 -0
- sage/structure/proof/proof.py +300 -0
- sage/structure/richcmp.cpython-314t-darwin.so +0 -0
- sage/structure/richcmp.pxd +213 -0
- sage/structure/richcmp.pyx +495 -0
- sage/structure/sage_object.cpython-314t-darwin.so +0 -0
- sage/structure/sage_object.pxd +3 -0
- sage/structure/sage_object.pyx +988 -0
- sage/structure/sage_object_test.py +19 -0
- sage/structure/sequence.py +937 -0
- sage/structure/set_factories.py +1178 -0
- sage/structure/set_factories_example.py +527 -0
- sage/structure/support_view.py +179 -0
- sage/structure/test_factory.py +56 -0
- sage/structure/unique_representation.py +1359 -0
sage/misc/prandom.py
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
# sage_setup: distribution = sagemath-objects
|
|
2
|
+
r"""
|
|
3
|
+
Random Numbers with Python API
|
|
4
|
+
|
|
5
|
+
AUTHORS:
|
|
6
|
+
-- Carl Witty (2008-03): new file
|
|
7
|
+
|
|
8
|
+
This module has the same functions as the Python standard module
|
|
9
|
+
\module{random}, but uses the current \sage random number state from
|
|
10
|
+
\module{sage.misc.randstate} (so that it can be controlled by the same
|
|
11
|
+
global random number seeds).
|
|
12
|
+
|
|
13
|
+
The functions here are less efficient than the functions in \module{random},
|
|
14
|
+
because they look up the current random number state on each call.
|
|
15
|
+
|
|
16
|
+
If you are going to be creating many random numbers in a row, it is
|
|
17
|
+
better to use the functions in \module{sage.misc.randstate} directly.
|
|
18
|
+
|
|
19
|
+
Here is an example:
|
|
20
|
+
|
|
21
|
+
(The imports on the next two lines are not necessary, since
|
|
22
|
+
\function{randrange} and \function{current_randstate} are both available
|
|
23
|
+
by default at the \code{sage:} prompt; but you would need them
|
|
24
|
+
to run these examples inside a module.) ::
|
|
25
|
+
|
|
26
|
+
sage: from sage.misc.prandom import randrange
|
|
27
|
+
sage: from sage.misc.randstate import current_randstate
|
|
28
|
+
sage: def test1():
|
|
29
|
+
....: return sum([randrange(100) for i in range(100)])
|
|
30
|
+
sage: def test2():
|
|
31
|
+
....: randrange = current_randstate().python_random().randrange
|
|
32
|
+
....: return sum([randrange(100) for i in range(100)])
|
|
33
|
+
|
|
34
|
+
Test2 will be slightly faster than test1, but they give the same answer::
|
|
35
|
+
|
|
36
|
+
sage: with seed(0): test1()
|
|
37
|
+
5169
|
|
38
|
+
sage: with seed(0): test2()
|
|
39
|
+
5169
|
|
40
|
+
sage: with seed(1): test1()
|
|
41
|
+
5097
|
|
42
|
+
sage: with seed(1): test2()
|
|
43
|
+
5097
|
|
44
|
+
sage: timeit('test1()') # random
|
|
45
|
+
625 loops, best of 3: 590 us per loop
|
|
46
|
+
sage: timeit('test2()') # random
|
|
47
|
+
625 loops, best of 3: 460 us per loop
|
|
48
|
+
|
|
49
|
+
The docstrings for the functions in this file are mostly copied from
|
|
50
|
+
Python's \file{random.py}, so those docstrings are "Copyright (c)
|
|
51
|
+
2001, 2002, 2003, 2004, 2005, 2006, 2007 Python Software Foundation;
|
|
52
|
+
All Rights Reserved" and are available under the terms of the
|
|
53
|
+
Python Software Foundation License Version 2.
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
# We deliberately omit "seed" and several other seed-related functions...
|
|
57
|
+
# setting seeds should only be done through sage.misc.randstate .
|
|
58
|
+
|
|
59
|
+
from sage.misc.randstate import current_randstate
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _pyrand():
|
|
63
|
+
r"""
|
|
64
|
+
A tiny private helper function to return an instance of
|
|
65
|
+
random.Random from the current \sage random number state.
|
|
66
|
+
Only for use in prandom.py; other modules should use
|
|
67
|
+
current_randstate().python_random().
|
|
68
|
+
|
|
69
|
+
EXAMPLES::
|
|
70
|
+
|
|
71
|
+
sage: set_random_seed(0)
|
|
72
|
+
sage: from sage.misc.prandom import _pyrand
|
|
73
|
+
sage: _pyrand()
|
|
74
|
+
<...random.Random object at 0x...>
|
|
75
|
+
sage: _pyrand().getrandbits(int(10))
|
|
76
|
+
114
|
|
77
|
+
"""
|
|
78
|
+
return current_randstate().python_random()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def getrandbits(k):
|
|
82
|
+
r"""
|
|
83
|
+
getrandbits(k) -> x. Generates a long int with k random bits.
|
|
84
|
+
|
|
85
|
+
EXAMPLES::
|
|
86
|
+
|
|
87
|
+
sage: getrandbits(10) in range(2^10)
|
|
88
|
+
True
|
|
89
|
+
sage: getrandbits(200) in range(2^200)
|
|
90
|
+
True
|
|
91
|
+
sage: getrandbits(4) in range(2^4)
|
|
92
|
+
True
|
|
93
|
+
"""
|
|
94
|
+
return _pyrand().getrandbits(int(k))
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def randrange(start, stop=None, step=1):
|
|
98
|
+
r"""
|
|
99
|
+
Choose a random item from range(start, stop[, step]).
|
|
100
|
+
|
|
101
|
+
This fixes the problem with randint() which includes the
|
|
102
|
+
endpoint; in Python this is usually not what you want.
|
|
103
|
+
|
|
104
|
+
EXAMPLES::
|
|
105
|
+
|
|
106
|
+
sage: s = randrange(0, 100, 11)
|
|
107
|
+
sage: 0 <= s < 100
|
|
108
|
+
True
|
|
109
|
+
sage: s % 11
|
|
110
|
+
0
|
|
111
|
+
|
|
112
|
+
sage: 5000 <= randrange(5000, 5100) < 5100
|
|
113
|
+
True
|
|
114
|
+
sage: s = [randrange(0, 2) for i in range(15)]
|
|
115
|
+
sage: all(t in [0, 1] for t in s)
|
|
116
|
+
True
|
|
117
|
+
|
|
118
|
+
sage: s = randrange(0, 1000000, 1000)
|
|
119
|
+
sage: 0 <= s < 1000000
|
|
120
|
+
True
|
|
121
|
+
sage: s % 1000
|
|
122
|
+
0
|
|
123
|
+
sage: -100 <= randrange(-100, 10) < 10
|
|
124
|
+
True
|
|
125
|
+
"""
|
|
126
|
+
return _pyrand().randrange(start, stop, step)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def randint(a, b):
|
|
130
|
+
r"""
|
|
131
|
+
Return random integer in range [a, b], including both end points.
|
|
132
|
+
|
|
133
|
+
EXAMPLES::
|
|
134
|
+
|
|
135
|
+
sage: s = [randint(0, 2) for i in range(15)]; s # random
|
|
136
|
+
[0, 1, 0, 0, 1, 0, 2, 0, 2, 1, 2, 2, 0, 2, 2]
|
|
137
|
+
sage: all(t in [0, 1, 2] for t in s)
|
|
138
|
+
True
|
|
139
|
+
sage: -100 <= randint(-100, 10) <= 10
|
|
140
|
+
True
|
|
141
|
+
"""
|
|
142
|
+
return _pyrand().randint(a, b)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def choice(seq):
|
|
146
|
+
r"""
|
|
147
|
+
Choose a random element from a non-empty sequence.
|
|
148
|
+
|
|
149
|
+
EXAMPLES::
|
|
150
|
+
|
|
151
|
+
sage: s = [choice(list(primes(10, 100))) for i in range(5)]; s # random # needs sage.libs.pari
|
|
152
|
+
[17, 47, 11, 31, 47]
|
|
153
|
+
sage: all(t in primes(10, 100) for t in s) # needs sage.libs.pari
|
|
154
|
+
True
|
|
155
|
+
"""
|
|
156
|
+
return _pyrand().choice(seq)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def shuffle(x):
|
|
160
|
+
r"""
|
|
161
|
+
x, random=random.random -> shuffle list x in place; return None.
|
|
162
|
+
|
|
163
|
+
Optional arg random is a 0-argument function returning a random
|
|
164
|
+
float in [0.0, 1.0); by default, the sage.misc.random.random.
|
|
165
|
+
|
|
166
|
+
EXAMPLES::
|
|
167
|
+
|
|
168
|
+
sage: shuffle([1 .. 10])
|
|
169
|
+
"""
|
|
170
|
+
return _pyrand().shuffle(x)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def sample(population, k):
|
|
174
|
+
r"""
|
|
175
|
+
Choose k unique random elements from a population sequence.
|
|
176
|
+
|
|
177
|
+
Return a new list containing elements from the population while
|
|
178
|
+
leaving the original population unchanged. The resulting list is
|
|
179
|
+
in selection order so that all sub-slices will also be valid random
|
|
180
|
+
samples. This allows raffle winners (the sample) to be partitioned
|
|
181
|
+
into grand prize and second place winners (the subslices).
|
|
182
|
+
|
|
183
|
+
Members of the population need not be hashable or unique. If the
|
|
184
|
+
population contains repeats, then each occurrence is a possible
|
|
185
|
+
selection in the sample.
|
|
186
|
+
|
|
187
|
+
To choose a sample in a range of integers, use xrange as an
|
|
188
|
+
argument (in Python 2) or range (in Python 3). This is especially
|
|
189
|
+
fast and space efficient for sampling from a large population:
|
|
190
|
+
sample(range(10000000), 60)
|
|
191
|
+
|
|
192
|
+
EXAMPLES::
|
|
193
|
+
|
|
194
|
+
sage: from sage.misc.misc import is_sublist
|
|
195
|
+
sage: l = ["Here", "I", "come", "to", "save", "the", "day"]
|
|
196
|
+
sage: s = sample(l, 3); s # random
|
|
197
|
+
['Here', 'to', 'day']
|
|
198
|
+
sage: is_sublist(sorted(s), sorted(l))
|
|
199
|
+
True
|
|
200
|
+
sage: len(s)
|
|
201
|
+
3
|
|
202
|
+
|
|
203
|
+
sage: s = sample(range(2^30), 7); s # random
|
|
204
|
+
[357009070, 558990255, 196187132, 752551188, 85926697, 954621491, 624802848]
|
|
205
|
+
sage: len(s)
|
|
206
|
+
7
|
|
207
|
+
sage: all(t in range(2^30) for t in s)
|
|
208
|
+
True
|
|
209
|
+
"""
|
|
210
|
+
return _pyrand().sample(population, k)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def random():
|
|
214
|
+
r"""
|
|
215
|
+
Get the next random number in the range [0.0, 1.0).
|
|
216
|
+
|
|
217
|
+
EXAMPLES::
|
|
218
|
+
|
|
219
|
+
sage: sample = [random() for i in [1 .. 4]]; sample # random
|
|
220
|
+
[0.111439293741037, 0.5143475134191677, 0.04468968524815642, 0.332490606442413]
|
|
221
|
+
sage: all(0.0 <= s <= 1.0 for s in sample)
|
|
222
|
+
True
|
|
223
|
+
"""
|
|
224
|
+
return _pyrand().random()
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def uniform(a, b):
|
|
228
|
+
r"""
|
|
229
|
+
Get a random number in the range [a, b).
|
|
230
|
+
|
|
231
|
+
Equivalent to \code{a + (b-a) * random()}.
|
|
232
|
+
|
|
233
|
+
EXAMPLES::
|
|
234
|
+
|
|
235
|
+
sage: s = uniform(0, 1); s # random
|
|
236
|
+
0.111439293741037
|
|
237
|
+
sage: 0.0 <= s <= 1.0
|
|
238
|
+
True
|
|
239
|
+
|
|
240
|
+
sage: s = uniform(e, pi); s # random # needs sage.symbolic
|
|
241
|
+
0.5143475134191677*pi + 0.48565248658083227*e
|
|
242
|
+
sage: bool(e <= s <= pi) # needs sage.symbolic
|
|
243
|
+
True
|
|
244
|
+
"""
|
|
245
|
+
return _pyrand().uniform(a, b)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def betavariate(alpha, beta):
|
|
249
|
+
r"""
|
|
250
|
+
Beta distribution.
|
|
251
|
+
|
|
252
|
+
Conditions on the parameters are alpha > 0 and beta > 0.
|
|
253
|
+
Returned values range between 0 and 1.
|
|
254
|
+
|
|
255
|
+
EXAMPLES::
|
|
256
|
+
|
|
257
|
+
sage: s = betavariate(0.1, 0.9); s # random
|
|
258
|
+
9.75087916621299e-9
|
|
259
|
+
sage: 0.0 <= s <= 1.0
|
|
260
|
+
True
|
|
261
|
+
|
|
262
|
+
sage: s = betavariate(0.9, 0.1); s # random
|
|
263
|
+
0.941890400939253
|
|
264
|
+
sage: 0.0 <= s <= 1.0
|
|
265
|
+
True
|
|
266
|
+
"""
|
|
267
|
+
return _pyrand().betavariate(alpha, beta)
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def expovariate(lambd):
|
|
271
|
+
r"""
|
|
272
|
+
Exponential distribution.
|
|
273
|
+
|
|
274
|
+
lambd is 1.0 divided by the desired mean. (The parameter would be
|
|
275
|
+
called "lambda", but that is a reserved word in Python.) Returned
|
|
276
|
+
values range from 0 to positive infinity.
|
|
277
|
+
|
|
278
|
+
EXAMPLES::
|
|
279
|
+
|
|
280
|
+
sage: sample = [expovariate(0.001) for i in range(3)]; sample # random
|
|
281
|
+
[118.152309288166, 722.261959038118, 45.7190543690470]
|
|
282
|
+
sage: all(s >= 0.0 for s in sample)
|
|
283
|
+
True
|
|
284
|
+
|
|
285
|
+
sage: sample = [expovariate(1.0) for i in range(3)]; sample # random
|
|
286
|
+
[0.404201816061304, 0.735220464997051, 0.201765578600627]
|
|
287
|
+
sage: all(s >= 0.0 for s in sample)
|
|
288
|
+
True
|
|
289
|
+
|
|
290
|
+
sage: sample = [expovariate(1000) for i in range(3)]; sample # random
|
|
291
|
+
[0.0012068700332283973, 8.340929747302108e-05, 0.00219877067980605]
|
|
292
|
+
sage: all(s >= 0.0 for s in sample)
|
|
293
|
+
True
|
|
294
|
+
"""
|
|
295
|
+
return _pyrand().expovariate(lambd)
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def gammavariate(alpha, beta):
|
|
299
|
+
r"""
|
|
300
|
+
Gamma distribution. (Not the gamma function.)
|
|
301
|
+
|
|
302
|
+
Conditions on the parameters are alpha > 0 and beta > 0.
|
|
303
|
+
|
|
304
|
+
EXAMPLES::
|
|
305
|
+
|
|
306
|
+
sage: sample = gammavariate(1.0, 3.0); sample # random
|
|
307
|
+
6.58282586130638
|
|
308
|
+
sage: sample > 0
|
|
309
|
+
True
|
|
310
|
+
sage: sample = gammavariate(3.0, 1.0); sample # random
|
|
311
|
+
3.07801512341612
|
|
312
|
+
sage: sample > 0
|
|
313
|
+
True
|
|
314
|
+
"""
|
|
315
|
+
return _pyrand().gammavariate(alpha, beta)
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def gauss(mu, sigma):
|
|
319
|
+
r"""
|
|
320
|
+
Gaussian distribution.
|
|
321
|
+
|
|
322
|
+
mu is the mean, and sigma is the standard deviation. This is
|
|
323
|
+
slightly faster than the normalvariate() function, but is not
|
|
324
|
+
thread-safe.
|
|
325
|
+
|
|
326
|
+
EXAMPLES::
|
|
327
|
+
|
|
328
|
+
sage: [gauss(0, 1) for i in range(3)] # random
|
|
329
|
+
[0.9191011757657915, 0.7744526756246484, 0.8638996866800877]
|
|
330
|
+
sage: [gauss(0, 100) for i in range(3)] # random
|
|
331
|
+
[24.916051749154448, -62.99272061579273, -8.1993122536718...]
|
|
332
|
+
sage: [gauss(1000, 10) for i in range(3)] # random
|
|
333
|
+
[998.7590700045661, 996.1087338511692, 1010.1256817458031]
|
|
334
|
+
"""
|
|
335
|
+
return _pyrand().gauss(mu, sigma)
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def lognormvariate(mu, sigma):
|
|
339
|
+
r"""
|
|
340
|
+
Log normal distribution.
|
|
341
|
+
|
|
342
|
+
If you take the natural logarithm of this distribution, you'll get a
|
|
343
|
+
normal distribution with mean mu and standard deviation sigma.
|
|
344
|
+
mu can have any value, and sigma must be greater than zero.
|
|
345
|
+
|
|
346
|
+
EXAMPLES::
|
|
347
|
+
|
|
348
|
+
sage: [lognormvariate(100, 10) for i in range(3)] # random
|
|
349
|
+
[2.9410355688290246e+37, 2.2257548162070125e+38, 4.142299451717446e+43]
|
|
350
|
+
"""
|
|
351
|
+
return _pyrand().lognormvariate(mu, sigma)
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
def normalvariate(mu, sigma):
|
|
355
|
+
r"""
|
|
356
|
+
Normal distribution.
|
|
357
|
+
|
|
358
|
+
mu is the mean, and sigma is the standard deviation.
|
|
359
|
+
|
|
360
|
+
EXAMPLES::
|
|
361
|
+
|
|
362
|
+
sage: [normalvariate(0, 1) for i in range(3)] # random
|
|
363
|
+
[-1.372558980559407, -1.1701670364898928, 0.04324100555110143]
|
|
364
|
+
sage: [normalvariate(0, 100) for i in range(3)] # random
|
|
365
|
+
[37.45695875041769, 159.6347743233298, 124.1029321124009]
|
|
366
|
+
sage: [normalvariate(1000, 10) for i in range(3)] # random
|
|
367
|
+
[1008.5303090383741, 989.8624892644895, 985.7728921150242]
|
|
368
|
+
"""
|
|
369
|
+
return _pyrand().normalvariate(mu, sigma)
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def vonmisesvariate(mu, kappa):
|
|
373
|
+
r"""
|
|
374
|
+
Circular data distribution.
|
|
375
|
+
|
|
376
|
+
mu is the mean angle, expressed in radians between 0 and 2*pi, and
|
|
377
|
+
kappa is the concentration parameter, which must be greater than or
|
|
378
|
+
equal to zero. If kappa is equal to zero, this distribution reduces
|
|
379
|
+
to a uniform random angle over the range 0 to 2*pi.
|
|
380
|
+
|
|
381
|
+
EXAMPLES::
|
|
382
|
+
|
|
383
|
+
sage: sample = [vonmisesvariate(1.0r, 3.0r) for i in range(1, 5)]; sample # random
|
|
384
|
+
[0.898328639355427, 0.6718030007041281, 2.0308777524813393, 1.714325253725145]
|
|
385
|
+
sage: all(s >= 0.0 for s in sample)
|
|
386
|
+
True
|
|
387
|
+
"""
|
|
388
|
+
return _pyrand().vonmisesvariate(mu, kappa)
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def paretovariate(alpha):
|
|
392
|
+
r"""
|
|
393
|
+
Pareto distribution. alpha is the shape parameter.
|
|
394
|
+
|
|
395
|
+
EXAMPLES::
|
|
396
|
+
|
|
397
|
+
sage: sample = [paretovariate(3) for i in range(1, 5)]; sample # random
|
|
398
|
+
[1.0401699394233033, 1.2722080162636495, 1.0153564009379579, 1.1442323078983077]
|
|
399
|
+
sage: all(s >= 1.0 for s in sample)
|
|
400
|
+
True
|
|
401
|
+
"""
|
|
402
|
+
return _pyrand().paretovariate(alpha)
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
def weibullvariate(alpha, beta):
|
|
406
|
+
r"""
|
|
407
|
+
Weibull distribution.
|
|
408
|
+
|
|
409
|
+
alpha is the scale parameter and beta is the shape parameter.
|
|
410
|
+
|
|
411
|
+
EXAMPLES::
|
|
412
|
+
|
|
413
|
+
sage: sample = [weibullvariate(1, 3) for i in range(1, 5)]; sample # random
|
|
414
|
+
[0.49069775546342537, 0.8972185564611213, 0.357573846531942, 0.739377255516847]
|
|
415
|
+
sage: all(s >= 0.0 for s in sample)
|
|
416
|
+
True
|
|
417
|
+
"""
|
|
418
|
+
return _pyrand().weibullvariate(alpha, beta)
|
|
Binary file
|
sage/misc/randstate.pxd
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# sage_setup: distribution = sagemath-objects
|
|
2
|
+
from sage.libs.gmp.types cimport gmp_randstate_t
|
|
3
|
+
|
|
4
|
+
# The c_random() method on randstate objects gives a value
|
|
5
|
+
# 0 <= n <= SAGE_RAND_MAX
|
|
6
|
+
cdef extern from *:
|
|
7
|
+
int SAGE_RAND_MAX "(0x7fffffff)" # 2^31 - 1
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
cdef class randstate:
|
|
11
|
+
cdef gmp_randstate_t gmp_state
|
|
12
|
+
cdef object _seed
|
|
13
|
+
cdef object _python_random
|
|
14
|
+
|
|
15
|
+
cdef object _gap_saved_seed
|
|
16
|
+
cdef object _pari_saved_seed
|
|
17
|
+
|
|
18
|
+
cdef object _gp_saved_seeds
|
|
19
|
+
|
|
20
|
+
cpdef set_seed_libc(self, bint force)
|
|
21
|
+
cpdef set_seed_ntl(self, bint force)
|
|
22
|
+
|
|
23
|
+
cpdef int c_random(self) noexcept
|
|
24
|
+
cpdef double c_rand_double(self) noexcept
|
|
25
|
+
|
|
26
|
+
cpdef ZZ_seed(self)
|
|
27
|
+
cpdef long_seed(self)
|
|
28
|
+
|
|
29
|
+
cpdef randstate current_randstate()
|
|
30
|
+
cpdef int random() noexcept
|