pyxmc 1.0.9__tar.gz

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.
@@ -0,0 +1,63 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Ivan Golubev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ Minimal Mistakes incorporates icons from [The Noun Project](https://thenounproject.com/)
24
+ creators Garrett Knoll, Arthur Shlain, and Tracy Tam.
25
+ Icons are distributed under Creative Commons Attribution 3.0 United States (CC BY 3.0 US).
26
+
27
+ Minimal Mistakes incorporates [Font Awesome](http://fontawesome.io/),
28
+ Copyright (c) 2017 Dave Gandy.
29
+ Font Awesome is distributed under the terms of the [SIL OFL 1.1](http://scripts.sil.org/OFL)
30
+ and [MIT License](http://opensource.org/licenses/MIT).
31
+
32
+ Minimal Mistakes incorporates photographs from [Unsplash](https://unsplash.com).
33
+
34
+ Minimal Mistakes incorporates [Susy](http://susy.oddbird.net/),
35
+ Copyright (c) 2017, Miriam Eric Suzanne.
36
+ Susy is distributed under the terms of the [BSD 3-clause "New" or "Revised" License](https://opensource.org/licenses/BSD-3-Clause).
37
+
38
+ Minimal Mistakes incorporates [Breakpoint](http://breakpoint-sass.com/).
39
+ Breakpoint is distributed under the terms of the [MIT/GPL Licenses](http://opensource.org/licenses/MIT).
40
+
41
+ Minimal Mistakes incorporates [FitVids.js](https://github.com/davatron5000/FitVids.js/),
42
+ Copyright (c) 2013 Dave Rubert and Chris Coyier.
43
+ FitVids is distributed under the terms of the [WTFPL License](http://sam.zoy.org/wtfpl/).
44
+
45
+ Minimal Mistakes incorporates [Magnific Popup](http://dimsemenov.com/plugins/magnific-popup/),
46
+ Copyright (c) 2014-2016 Dmitry Semenov, http://dimsemenov.com.
47
+ Magnific Popup is distributed under the terms of the MIT License.
48
+
49
+ Minimal Mistakes incorporates [jQuery Smooth Scroll](https://github.com/kswedberg/jquery-smooth-scroll),
50
+ Copyright (c) 2017 Karl Swedberg.
51
+ jQuery Smooth Scroll is distributed under the terms of the [MIT License](http://opensource.org/licenses/MIT).
52
+
53
+ Minimal Mistakes incorporates [GreedyNav.js](https://github.com/lukejacksonn/GreedyNav),
54
+ Copyright (c) 2015 Luke Jackson.
55
+ GreedyNav.js is distributed under the terms of the [MIT License](http://opensource.org/licenses/MIT).
56
+
57
+ Minimal Mistakes incorporates [Jekyll Group-By-Array](https://github.com/mushishi78/jekyll-group-by-array),
58
+ Copyright (c) 2015 Max White <mushishi78@gmail.com>.
59
+ Jekyll Group-By-Array is distributed under the terms of the [MIT License](http://opensource.org/licenses/MIT).
60
+
61
+ Minimal Mistakes incorporates [Lunr](http://lunrjs.com),
62
+ Copyright (c) 2017 Oliver Nightingale.
63
+ Lunr is distributed under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,7 @@
1
+ include README.md LICENSE.txt pyproject.toml setup.py
2
+ recursive-include src *.c *.h
3
+ recursive-include python/xmc *.py *.pyi
4
+ prune python/xmc/__pycache__
5
+ prune python/xmc.egg-info
6
+ global-exclude *.so *.pyc
7
+ exclude python/use.py
pyxmc-1.0.9/PKG-INFO ADDED
@@ -0,0 +1,57 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyxmc
3
+ Version: 1.0.9
4
+ Summary: xoshiro256 RNG + Monte-Carlo (C-extension).
5
+ Author: Ivan Golubev
6
+ License-Expression: MIT
7
+ Classifier: Programming Language :: C
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Requires-Python: >=3.11
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE.txt
15
+ Provides-Extra: dev
16
+ Requires-Dist: pytest>=7; extra == "dev"
17
+ Requires-Dist: numpy; extra == "dev"
18
+ Dynamic: license-file
19
+
20
+ # pyxmc
21
+
22
+ `pyxmc` это расширение для Python на C, реализующее
23
+ быстрый PRNG (xoshiro256) и базовые методы Монте-Карло.
24
+
25
+ ## Функционал
26
+
27
+ - `xmc.RNG`: структура, реализующая генерацию псевдорандомных чисел.
28
+ - `xmc.estimate_pi(n, seed=0)`: оценка числа π методом Монте-Карло.
29
+
30
+ ## Установка
31
+
32
+ ```bash
33
+ python3.11 -m venv .venv
34
+ source .venv/bin/activate
35
+ pip install -U pip setuptools wheel
36
+ pip install -e ".[dev]"
37
+ pytest -q
38
+ ```
39
+
40
+ ## Требования
41
+
42
+ - Python 3.11+
43
+ - GCC/clang, поддержка C11
44
+ - Linux/macOS
45
+
46
+ ## Использование
47
+
48
+ ```python
49
+ import xmc # version 1.0.0
50
+
51
+ r = xmc.RNG(seed=42)
52
+ print(r.random())
53
+ print(r.randint(1, 6))
54
+ print(repr(r))
55
+
56
+ print(xmc.estimate_pi(10_000_000))
57
+ ```
pyxmc-1.0.9/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # pyxmc
2
+
3
+ `pyxmc` это расширение для Python на C, реализующее
4
+ быстрый PRNG (xoshiro256) и базовые методы Монте-Карло.
5
+
6
+ ## Функционал
7
+
8
+ - `xmc.RNG`: структура, реализующая генерацию псевдорандомных чисел.
9
+ - `xmc.estimate_pi(n, seed=0)`: оценка числа π методом Монте-Карло.
10
+
11
+ ## Установка
12
+
13
+ ```bash
14
+ python3.11 -m venv .venv
15
+ source .venv/bin/activate
16
+ pip install -U pip setuptools wheel
17
+ pip install -e ".[dev]"
18
+ pytest -q
19
+ ```
20
+
21
+ ## Требования
22
+
23
+ - Python 3.11+
24
+ - GCC/clang, поддержка C11
25
+ - Linux/macOS
26
+
27
+ ## Использование
28
+
29
+ ```python
30
+ import xmc # version 1.0.0
31
+
32
+ r = xmc.RNG(seed=42)
33
+ print(r.random())
34
+ print(r.randint(1, 6))
35
+ print(repr(r))
36
+
37
+ print(xmc.estimate_pi(10_000_000))
38
+ ```
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "pyxmc"
7
+ version = "1.0.9"
8
+ description = "xoshiro256 RNG + Monte-Carlo (C-extension)."
9
+ readme = "README.md"
10
+ requires-python = ">=3.11"
11
+ license = "MIT"
12
+ authors = [{ name = "Ivan Golubev" }]
13
+ classifiers = [
14
+ "Programming Language :: C",
15
+ "Programming Language :: Python :: 3",
16
+ "Programming Language :: Python :: 3.11",
17
+ "Programming Language :: Python :: 3.12",
18
+ "Programming Language :: Python :: 3.13",
19
+ ]
20
+
21
+ [project.optional-dependencies]
22
+ dev = ["pytest>=7", "numpy"]
23
+
24
+ [tool.setuptools]
25
+ package-dir = { "" = "python" }
26
+ packages = ["xmc"]
27
+
28
+ [tool.setuptools.package-data]
29
+ xmc = ["*.pyi"]
30
+
31
+ [tool.pytest.ini_options]
32
+ testpaths = ["tests"]
@@ -0,0 +1,57 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyxmc
3
+ Version: 1.0.9
4
+ Summary: xoshiro256 RNG + Monte-Carlo (C-extension).
5
+ Author: Ivan Golubev
6
+ License-Expression: MIT
7
+ Classifier: Programming Language :: C
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Requires-Python: >=3.11
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE.txt
15
+ Provides-Extra: dev
16
+ Requires-Dist: pytest>=7; extra == "dev"
17
+ Requires-Dist: numpy; extra == "dev"
18
+ Dynamic: license-file
19
+
20
+ # pyxmc
21
+
22
+ `pyxmc` это расширение для Python на C, реализующее
23
+ быстрый PRNG (xoshiro256) и базовые методы Монте-Карло.
24
+
25
+ ## Функционал
26
+
27
+ - `xmc.RNG`: структура, реализующая генерацию псевдорандомных чисел.
28
+ - `xmc.estimate_pi(n, seed=0)`: оценка числа π методом Монте-Карло.
29
+
30
+ ## Установка
31
+
32
+ ```bash
33
+ python3.11 -m venv .venv
34
+ source .venv/bin/activate
35
+ pip install -U pip setuptools wheel
36
+ pip install -e ".[dev]"
37
+ pytest -q
38
+ ```
39
+
40
+ ## Требования
41
+
42
+ - Python 3.11+
43
+ - GCC/clang, поддержка C11
44
+ - Linux/macOS
45
+
46
+ ## Использование
47
+
48
+ ```python
49
+ import xmc # version 1.0.0
50
+
51
+ r = xmc.RNG(seed=42)
52
+ print(r.random())
53
+ print(r.randint(1, 6))
54
+ print(repr(r))
55
+
56
+ print(xmc.estimate_pi(10_000_000))
57
+ ```
@@ -0,0 +1,22 @@
1
+ LICENSE.txt
2
+ MANIFEST.in
3
+ README.md
4
+ pyproject.toml
5
+ setup.py
6
+ python/pyxmc.egg-info/PKG-INFO
7
+ python/pyxmc.egg-info/SOURCES.txt
8
+ python/pyxmc.egg-info/dependency_links.txt
9
+ python/pyxmc.egg-info/requires.txt
10
+ python/pyxmc.egg-info/top_level.txt
11
+ python/xmc/__init__.py
12
+ python/xmc/__init__.pyi
13
+ src/module.c
14
+ src/mc/mc.c
15
+ src/mc/mc.h
16
+ src/rng/rng.c
17
+ src/rng/rng.h
18
+ src/rng/rng_core.c
19
+ src/rng/rng_core.h
20
+ src/util/errors.h
21
+ tests/test_mc.py
22
+ tests/test_rng.py
@@ -0,0 +1,4 @@
1
+
2
+ [dev]
3
+ pytest>=7
4
+ numpy
@@ -0,0 +1 @@
1
+ xmc
@@ -0,0 +1,5 @@
1
+ """xmc: xoshiro256 RNG + Monte-Carlo."""
2
+
3
+ from ._xmc import RNG, __version__, estimate_pi
4
+
5
+ __all__ = ["RNG", "__version__", "estimate_pi"]
@@ -0,0 +1,12 @@
1
+ from typing import final
2
+
3
+ __version__: str
4
+
5
+ @final
6
+ class RNG:
7
+ def __init__(self, seed: int = 0) -> None: ...
8
+ def seed(self, s: int) -> None: ...
9
+ def random(self) -> float: ...
10
+ def randint(self, a: int, b: int) -> int: ...
11
+
12
+ def estimate_pi(n: int, seed: int = 0) -> float: ...
pyxmc-1.0.9/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
pyxmc-1.0.9/setup.py ADDED
@@ -0,0 +1,15 @@
1
+ from setuptools import Extension, setup
2
+
3
+ ext = Extension(
4
+ "xmc._xmc",
5
+ sources=[
6
+ "src/module.c",
7
+ "src/rng/rng.c",
8
+ "src/rng/rng_core.c",
9
+ "src/mc/mc.c",
10
+ ],
11
+ include_dirs=["src"],
12
+ extra_compile_args=["-O3", "-std=c11", "-Wall", "-Wextra"],
13
+ )
14
+
15
+ setup(ext_modules=[ext])
@@ -0,0 +1,43 @@
1
+ #include "mc/mc.h"
2
+ #include "rng/rng_core.h"
3
+
4
+ static PyObject *XmcMC_estimate_pi(PyObject *Py_UNUSED(self), PyObject *args,
5
+ PyObject *kwds) {
6
+
7
+ static char *kwlist[] = {"n", "seed", NULL};
8
+ unsigned long long n;
9
+ unsigned long long seed = 0;
10
+
11
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "K|K", kwlist, &n, &seed)) {
12
+ return NULL;
13
+ }
14
+
15
+ xmc_xoshiro_state_t st;
16
+ xmc_xoshiro_seed(&st, (uint64_t)seed);
17
+
18
+ /* Throw n random points into unit square and count. */
19
+ unsigned long long inside = 0;
20
+
21
+ for (unsigned long long i = 0; i < n; ++i) {
22
+
23
+ double x = xmc_xoshiro_next_double(&st);
24
+ double y = xmc_xoshiro_next_double(&st);
25
+
26
+ if (x * x + y * y <= 1.0) {
27
+ inside++;
28
+ }
29
+ }
30
+
31
+ return PyFloat_FromDouble(4.0 * (double)inside / (double)n);
32
+ }
33
+
34
+ static PyMethodDef XmcMC_methods[] = {
35
+ {"estimate_pi", _PyCFunction_CAST(XmcMC_estimate_pi),
36
+ METH_VARARGS | METH_KEYWORDS,
37
+ "estimate_pi(n, seed=0) -> float\n\n"
38
+ "Monte Carlo estimate of pi (single-threaded)."},
39
+ {NULL, NULL, 0, NULL}};
40
+
41
+ int XmcMC_Register(PyObject *module) {
42
+ return PyModule_AddFunctions(module, XmcMC_methods);
43
+ }
@@ -0,0 +1,9 @@
1
+ #ifndef XMC_MC_H
2
+ #define XMC_MC_H
3
+
4
+ #include <Python.h>
5
+
6
+ /* Register Monte-Carlo functions in module. */
7
+ int XmcMC_Register(PyObject *module);
8
+
9
+ #endif /* XMC_MC_H */
@@ -0,0 +1,36 @@
1
+ #include <Python.h>
2
+
3
+ #include "mc/mc.h"
4
+ #include "rng/rng.h"
5
+
6
+ static struct PyModuleDef xmc_module = {
7
+ PyModuleDef_HEAD_INIT,
8
+ .m_name = "_xmc",
9
+ .m_doc = "xmc: xoshiro256 RNG + Monte-Carlo.",
10
+ .m_size = -1,
11
+ .m_methods = NULL,
12
+ };
13
+
14
+ PyMODINIT_FUNC PyInit__xmc(void) {
15
+ PyObject *m = PyModule_Create(&xmc_module);
16
+ if (m == NULL) {
17
+ return NULL;
18
+ }
19
+
20
+ if (XmcRNG_Register(m) < 0) {
21
+ Py_DECREF(m);
22
+ return NULL;
23
+ }
24
+
25
+ if (XmcMC_Register(m) < 0) {
26
+ Py_DECREF(m);
27
+ return NULL;
28
+ }
29
+
30
+ if (PyModule_AddStringConstant(m, "__version__", "0.1.0") < 0) {
31
+ Py_DECREF(m);
32
+ return NULL;
33
+ }
34
+
35
+ return m;
36
+ }
@@ -0,0 +1,136 @@
1
+ #include "rng/rng.h"
2
+ #include "util/errors.h"
3
+
4
+ #include <string.h>
5
+
6
+ // tp_new: magic method __new__
7
+
8
+ static PyObject *XmcRNG_new(PyTypeObject *type, PyObject *args,
9
+ PyObject *kwds) {
10
+
11
+ static char *kwlist[] = {"seed", NULL};
12
+ unsigned long long seed = 0;
13
+
14
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|K", kwlist, &seed)) {
15
+ return NULL;
16
+ }
17
+
18
+ XmcRNG *self = (XmcRNG *)type->tp_alloc(type, 0);
19
+ if (self == NULL) {
20
+ return NULL;
21
+ }
22
+
23
+ self->state = PyMem_Malloc(sizeof(xmc_xoshiro_state_t));
24
+ if (self->state == NULL) {
25
+ Py_DECREF(self);
26
+ return PyErr_NoMemory();
27
+ }
28
+
29
+ xmc_xoshiro_seed(self->state, (uint64_t)seed);
30
+
31
+ return (PyObject *)self;
32
+ }
33
+
34
+ // tp_dealloc
35
+
36
+ static void XmcRNG_dealloc(XmcRNG *self) {
37
+ if (self->state != NULL) {
38
+ /* Zero out state before free. */
39
+ memset(self->state, 0, sizeof(xmc_xoshiro_state_t));
40
+ PyMem_Free(self->state);
41
+ self->state = NULL;
42
+ }
43
+ Py_TYPE(self)->tp_free((PyObject *)self);
44
+ }
45
+
46
+ // tp_methods
47
+
48
+ static PyObject *XmcRNG_seed(XmcRNG *self, PyObject *arg) {
49
+
50
+ unsigned long long seed = PyLong_AsUnsignedLongLong(arg);
51
+ if (PyErr_Occurred()) {
52
+ return NULL;
53
+ }
54
+
55
+ xmc_xoshiro_seed(self->state, (uint64_t)seed);
56
+
57
+ Py_RETURN_NONE;
58
+ /* Equivalent to: Py_INCREF(Py_None); return Py_None; */
59
+ }
60
+
61
+ static PyObject *XmcRNG_random(XmcRNG *self, PyObject *Py_UNUSED(ignored)) {
62
+ return PyFloat_FromDouble(xmc_xoshiro_next_double(self->state));
63
+ }
64
+
65
+ static PyObject *XmcRNG_randint(XmcRNG *self, PyObject *args) {
66
+
67
+ unsigned long long a, b;
68
+ if (!PyArg_ParseTuple(args, "KK", &a, &b)) {
69
+ return NULL;
70
+ }
71
+
72
+ if (a > b) {
73
+ XMC_RETURN_VALUE_ERR("randint: a must be <= b");
74
+ return NULL;
75
+ }
76
+
77
+ /* randint(a, b) is inclusive on both sides. */
78
+ unsigned long long bound = b - a + 1ULL;
79
+
80
+ if (bound == 0) {
81
+ /* Overflow: full uint64 range. */
82
+ return PyLong_FromUnsignedLongLong(xmc_xoshiro_next(self->state));
83
+ }
84
+
85
+ unsigned long long r = xmc_xoshiro_next_bounded(self->state, bound);
86
+
87
+ return PyLong_FromUnsignedLongLong(a + r);
88
+ }
89
+
90
+ // tp_repr: magic method __repr__
91
+
92
+ static PyObject *XmcRNG_repr(XmcRNG *self) {
93
+ return PyUnicode_FromFormat("<xmc.RNG state=%p s0=%llu>", (void *)self,
94
+ (unsigned long long)self->state->s[0]);
95
+ }
96
+
97
+ static PyObject *XmcRNG_str(XmcRNG *self) {
98
+ return PyUnicode_FromFormat("RNG(s0=%llu)",
99
+ (unsigned long long)self->state->s[0]);
100
+ }
101
+
102
+ // tables
103
+
104
+ static PyMethodDef XmcRNG_methods[] = {
105
+ {"seed", (PyCFunction)XmcRNG_seed, METH_O,
106
+ "seed(s) -> None\n\nReseed the generator."},
107
+ {"random", (PyCFunction)XmcRNG_random, METH_NOARGS,
108
+ "random() -> float in [0.0, 1.0)\n\nNext uniform value."},
109
+ {"randint", (PyCFunction)XmcRNG_randint, METH_VARARGS,
110
+ "randint(a, b) -> int in [a, b]\n\nInclusive integer in range."},
111
+ {NULL, NULL, 0, NULL}};
112
+
113
+ PyTypeObject XmcRNG_Type = {
114
+ PyVarObject_HEAD_INIT(NULL, 0).tp_name = "xmc.RNG",
115
+ .tp_basicsize = sizeof(XmcRNG),
116
+ .tp_itemsize = 0,
117
+ .tp_dealloc = (destructor)XmcRNG_dealloc,
118
+ .tp_repr = (reprfunc)XmcRNG_repr,
119
+ .tp_str = (reprfunc)XmcRNG_str,
120
+ .tp_flags = Py_TPFLAGS_DEFAULT,
121
+ .tp_doc = "xoshiro256** random number generator.",
122
+ .tp_methods = XmcRNG_methods,
123
+ .tp_new = XmcRNG_new,
124
+ };
125
+
126
+ int XmcRNG_Register(PyObject *module) {
127
+ if (PyType_Ready(&XmcRNG_Type) < 0) {
128
+ return -1;
129
+ }
130
+
131
+ if (PyModule_AddObjectRef(module, "RNG", (PyObject *)&XmcRNG_Type) < 0) {
132
+ return -1;
133
+ }
134
+
135
+ return 0;
136
+ }
@@ -0,0 +1,19 @@
1
+ #ifndef XMC_RNG_H
2
+ #define XMC_RNG_H
3
+
4
+ #include "rng/rng_core.h"
5
+ #include <Python.h>
6
+
7
+ /* Python type: xmc.RNG */
8
+ typedef struct {
9
+ PyObject_HEAD
10
+ /* ob_refcnt + *ob_type, see Python API docs. */
11
+ xmc_xoshiro_state_t *state;
12
+ } XmcRNG;
13
+
14
+ extern PyTypeObject XmcRNG_Type;
15
+
16
+ /* Register type in module. Returns 0 on success, -1 on error. */
17
+ int XmcRNG_Register(PyObject *module);
18
+
19
+ #endif /* XMC_RNG_H */
@@ -0,0 +1,65 @@
1
+ #include "rng/rng_core.h"
2
+
3
+ /* SplitMix64: only used for seeding. */
4
+ static uint64_t splitmix64(uint64_t *x) {
5
+ uint64_t z = (*x += 0x9E3779B97F4A7C15ULL);
6
+
7
+ z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
8
+ z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
9
+
10
+ return z ^ (z >> 31);
11
+ }
12
+
13
+ static uint64_t rotl(uint64_t x, int k) { return (x << k) | (x >> (64 - k)); }
14
+
15
+ void xmc_xoshiro_seed(xmc_xoshiro_state_t *state, uint64_t seed) {
16
+ uint64_t x = seed;
17
+ state->s[0] = splitmix64(&x);
18
+ state->s[1] = splitmix64(&x);
19
+ state->s[2] = splitmix64(&x);
20
+ state->s[3] = splitmix64(&x);
21
+ }
22
+
23
+ uint64_t xmc_xoshiro_next(xmc_xoshiro_state_t *state) {
24
+ uint64_t *s = state->s;
25
+ uint64_t const result = rotl(s[1] * 5, 7) * 9;
26
+ uint64_t const t = s[1] << 17;
27
+
28
+ s[2] ^= s[0];
29
+ s[3] ^= s[1];
30
+ s[1] ^= s[2];
31
+ s[0] ^= s[3];
32
+ s[2] ^= t;
33
+
34
+ s[3] = rotl(s[3], 45);
35
+
36
+ return result;
37
+ }
38
+
39
+ double xmc_xoshiro_next_double(xmc_xoshiro_state_t *state) {
40
+ /* Top 53 bits -> [0, 1). 2^-53 = 1 / 9007199254740992. */
41
+ uint64_t x = xmc_xoshiro_next(state);
42
+ return (double)(x >> 11) * (1.0 / 9007199254740992.0);
43
+ }
44
+
45
+ uint64_t xmc_xoshiro_next_bounded(xmc_xoshiro_state_t *state, uint64_t bound) {
46
+ /* Lemire's nearly divisionless method. */
47
+ if (bound == 0) {
48
+ return 0;
49
+ }
50
+
51
+ uint64_t x = xmc_xoshiro_next(state);
52
+ __uint128_t m = (__uint128_t)x * (__uint128_t)bound;
53
+ uint64_t l = (uint64_t)m;
54
+
55
+ if (l < bound) {
56
+ uint64_t t = (uint64_t)(-bound) % bound;
57
+ while (l < t) {
58
+ x = xmc_xoshiro_next(state);
59
+ m = (__uint128_t)x * (__uint128_t)bound;
60
+ l = (uint64_t)m;
61
+ }
62
+ }
63
+
64
+ return (uint64_t)(m >> 64);
65
+ }
@@ -0,0 +1,28 @@
1
+ #ifndef XMC_RNG_CORE_H
2
+ #define XMC_RNG_CORE_H
3
+
4
+ #include <stdint.h>
5
+
6
+ /* xoshiro256 PRNG. Public domain (CC0) by Blackman & Vigna.
7
+ * Reference: http://prng.di.unimi.it/xoshiro256starstar.c
8
+ * State = 4 x uint64_t.
9
+ * Period = 2^256 - 1.
10
+ */
11
+
12
+ typedef struct {
13
+ uint64_t s[4];
14
+ } xmc_xoshiro_state_t;
15
+
16
+ /* Seed via SplitMix64 (recommended by authors). */
17
+ void xmc_xoshiro_seed(xmc_xoshiro_state_t *state, uint64_t seed);
18
+
19
+ /* Next 64-bit output. */
20
+ uint64_t xmc_xoshiro_next(xmc_xoshiro_state_t *state);
21
+
22
+ /* Uniform double in [0, 1) using top 53 bits. */
23
+ double xmc_xoshiro_next_double(xmc_xoshiro_state_t *state);
24
+
25
+ /* Uniform uint64 in [0, bound) via Lemire's method. */
26
+ uint64_t xmc_xoshiro_next_bounded(xmc_xoshiro_state_t *state, uint64_t bound);
27
+
28
+ #endif /* XMC_RNG_CORE_H */
@@ -0,0 +1,12 @@
1
+ #ifndef XMC_UTIL_ERRORS_H
2
+ #define XMC_UTIL_ERRORS_H
3
+
4
+ #include <Python.h>
5
+ #include <errno.h>
6
+
7
+ #define XMC_RETURN_ERRNO(msg) \
8
+ PyErr_SetFromErrnoWithFilename(PyExc_OSError, (msg))
9
+
10
+ #define XMC_RETURN_VALUE_ERR(msg) PyErr_SetString(PyExc_ValueError, (msg))
11
+
12
+ #endif /* XMC_UTIL_ERRORS_H */
@@ -0,0 +1,13 @@
1
+ import math
2
+
3
+ import xmc
4
+
5
+
6
+ def test_pi_rough():
7
+ # n=1e6 gives stderr around 0.0005
8
+ v = xmc.estimate_pi(1_000_000, seed=42)
9
+ assert abs(v - math.pi) < 0.01
10
+
11
+
12
+ def test_pi_deterministic_with_seed():
13
+ assert xmc.estimate_pi(100_000, seed=123) == xmc.estimate_pi(100_000, seed=123)
@@ -0,0 +1,35 @@
1
+ import xmc
2
+
3
+
4
+ def reproducible_with_same_seed():
5
+ a = xmc.RNG(42)
6
+ b = xmc.RNG(42)
7
+
8
+ assert [a.random() for _ in range(10)] == [b.random() for _ in range(10)]
9
+
10
+
11
+ def different_seeds():
12
+ assert xmc.RNG(1).random() != xmc.RNG(2).random()
13
+
14
+
15
+ def random_in_unit():
16
+ r = xmc.RNG(0)
17
+ for _ in range(1000):
18
+ assert 0.0 <= r.random() < 1.0
19
+
20
+
21
+ def randint_bounds():
22
+ r = xmc.RNG(123)
23
+ vals = [r.randint(5, 10) for _ in range(1000)]
24
+
25
+ assert min(vals) >= 5
26
+ assert max(vals) <= 10
27
+ assert set(vals) == {5, 6, 7, 8, 9, 10}
28
+
29
+
30
+ def seed_method_resets():
31
+ r = xmc.RNG(7)
32
+ first = r.random()
33
+ r.seed(7)
34
+
35
+ assert r.random() == first