lemire / fastrand Goto Github PK
View Code? Open in Web Editor NEWFast random number generation in an interval in Python: Up to 10x faster than random.randint.
License: Apache License 2.0
Fast random number generation in an interval in Python: Up to 10x faster than random.randint.
License: Apache License 2.0
Hi,
I'm a little novice, who would like to solve the following little problem:
fastrand
(bounded for instance by 1001).I mean:
Regards,
it seems that the numbers that are generated are the same every time a program is run.
Following this issue...
#9
We should now be able to do "pip install fastrand" on Linux and Windows. However, macOS remains unsupported.
Is there a way to set our own seed for reproducibility purposes? Similarly for ensuring different streams while parallelizing code...
When installing for Python3.5, at import time it gives an error:
undefined symbol: PyInt_AsUnsignedLongLongMask
to solve it, I had to modify the .c file from:
#if PY_MAJOR_VERSION >= 3
#define PyInt_AsLong(x) PyLong_AsLong(x)
#endif
to
#if PY_MAJOR_VERSION >= 3
#define PyInt_AsLong(x) PyLong_AsLong(x)
#define PyInt_AsUnsignedLongLongMask(x) PyLong_AsUnsignedLongLongMask(x)
#endif
It would be great if you could upload the package so that it can be installed via pip install fastrand
. The current process is a bit tedious.
Add an option to specify a lower bound for the random int generation.
Examples from numpy and the random lib in python below.
numpy.random.randint(950, 1000)
random.randint(100,1000)
This would make fastrand a better replacement for quickly generating random ints.
I peek at the code pcg32_random_bounded_divisionless().
Why variable 'leftover' able to reject unbiased randoms ?
No bias if leftover >= range or leftover(updated) >= threshold.
I don't follow above logic ...
Does this produce unbiased ranged number ?
Or, is it an approximation, using your idea in another blog:
https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
Error:
Collecting fastrand
Downloading https://files.pythonhosted.org/packages/4a/c9/84ae3fa10cb24bd5ac88845a55ddddc572d165218e29613063763f12d8c4/fastrand-1.7.0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-lr57d0c2/fastrand/setup.py", line 9, in <module>
long_description=open('README.md', 'r').read(),
File "/usr/lib/python3.5/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 124: ordinal not in range(128)
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-lr57d0c2/fastrand/
Any way to fix this?
I know this is not officially supported but is there any way to install this library on Windows?
The C function pcg32_random()
returns an unsigned uint32_t
, but the pcg32()
function exposed to Python calls Py_BuildValue()
with "i"
, which causes it to return a signed int
(docs). Is this a bug or was it intentional? I don't see this explicitly mentioned anywhere, but I assumed it would give me unsigned 32-bit integers, and I was surprised to see it giving me negative numbers.
On latest python 2.7 your library is faster
python -m timeit -s 'import fastrand' 'fastrand.pcg32bounded(1001)'
10000000 loops, best of 3: 0.0994 usec per loop
python -m timeit -s 'import numpy' 'numpy.random.randint(0, 1000)'
1000000 loops, best of 3: 1.07 usec per loop
so, I think this should be removed from readme :)
David Andersen points out that
python -m timeit -s 'import numpy' 'numpy.random.randint(0, 1000)'
is much faster.
Hi to all,
We tried to download latest version of fastrand 1.2.
However "pip install fastrand" ended up installing 1.1
"pip install fastrand --upgrade" also ended up installing 1.1
What can be the reason for that?
Best,
Burcak Otlu
Subj.
?
Thank you for your really fast random number generator.
;)
To reproduce:
python -m timeit -s 'import fastrand' 'fastrand.pcg32_seed(13)'
Fatal Python error: deallocating None
Current thread 0x00000001183f3dc0 (most recent call first):
File "<timeit-src>", line 6 in inner
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/timeit.py", line 176 in timeit
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/timeit.py", line 222 in autorange
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/timeit.py", line 324 in main
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/timeit.py", line 374 in <module>
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 85 in _run_code
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 193 in _run_module_as_main
zsh: abort python -m timeit -s 'import fastrand' 'fastrand.pcg32_seed(13)'
Or from within python:
import fastrand; rngs = [fastrand.pcg32_seed(i) for i in range(100000)]
Fatal Python error: deallocating None
Current thread 0x0000000116acadc0 (most recent call first):
File "<stdin>", line 1 in <module>
zsh: abort python
Verified that it happens on multiple OSes (MacOS, ScientificLinux).
To match pcg32bounded() doc-string (below), I propose to patch argument check.
Sorry, this should be a pull-request, but I don't know how to use it
generate random integer in the interval [0,range) using PCG.
Since pcg32bounded only allowed 1 argument, the code can be optimized
without overhead of parsing tuple. Just go straight for the number.
This is the full patch:
56,58c56,58
< int n;
< if (!PyArg_ParseTuple(args, "i", &n))
< return NULL;
---
> int n = PyLong_AsLong(args); // 1 argument, ok for Python 2 or 3
> if (n <= 0) return PyErr_Occurred() ? NULL : (Py_INCREF(Py_None), Py_None);
>
98c98
< {"pcg32bounded", pcg32bounded, METH_VARARGS, "generate random integer in
the interval [0,range) using PCG."},
---
> {"pcg32bounded", pcg32bounded, METH_O, "generate random integer in the in
terval [0,range) using PCG."},
With the patch, this is benchmark on my laptop:
> python -m timeit -s "import fastrand" "fastrand.pcg32bounded(1001)"
1000000 loops, best of 3: 0.208 usec per loop -- original
10000000 loops, best of 3: 0.144 usec per loop -- patched
Error messages now as expected:
>>> from fastrand import *
>>> print pcg32bounded(1001)
82
>>> print pcg32bounded(0)
None # should not return anything
>>> print pcg32bounded(2**31-1), pcg32bounded(-2**31)
20852737 None # max input limits
>>> print pcg32bounded(2**31) # overflow
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to int
>>> print pcg32bounded('Hi') # bad integer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.