ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 14 Jul 2014 06:55:07 +0200Fast (uniform) random integer generationhttps://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/I'm doing some largish experiments involving randomness, and I think random number generation may be the bottleneck. Is there a way to make the random number generator significantly faster without sacrificing the validity of results? I don't especially care that the RNG be cryptographically secure or anything like that.Fri, 11 Jul 2014 19:34:54 +0200https://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/Answer by vdelecroix for <p>I'm doing some largish experiments involving randomness, and I think random number generation may be the bottleneck. Is there a way to make the random number generator significantly faster without sacrificing the validity of results? I don't especially care that the RNG be cryptographically secure or anything like that.</p>
https://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/?answer=23392#post-id-23392Hello,
Just to complete Thierry, answer I guess the most sageish way would be
sage: ZZ.random_element(0,10)
3
It is as fast as numpy and has the advantage of supporting arbitrary precision number. If you want something really fast see how ZZ.random_element is implemented using cython and gmp integers.
VincentSat, 12 Jul 2014 21:45:59 +0200https://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/?answer=23392#post-id-23392Comment by Anschel Schaffer-Cohen for <p>Hello,</p>
<p>Just to complete Thierry, answer I guess the most sageish way would be</p>
<pre><code>sage: ZZ.random_element(0,10)
3
</code></pre>
<p>It is as fast as numpy and has the advantage of supporting arbitrary precision number. If you want something really fast see how ZZ.random_element is implemented using cython and gmp integers.</p>
<p>Vincent</p>
https://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/?comment=23396#post-id-23396It doesn't actually seem to be as fast as numpy, which runs in about a 40th of the time in my benchmark.Sun, 13 Jul 2014 05:56:46 +0200https://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/?comment=23396#post-id-23396Answer by tmonteil for <p>I'm doing some largish experiments involving randomness, and I think random number generation may be the bottleneck. Is there a way to make the random number generator significantly faster without sacrificing the validity of results? I don't especially care that the RNG be cryptographically secure or anything like that.</p>
https://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/?answer=23379#post-id-23379Sage includes ``numpy``, you can compare the different timings:
sage: %timeit [random.randint(0,10) for i in xrange(1000)]
100 loops, best of 3: 5.61 ms per loop
sage: %timeit [random.randrange(0,11) for i in xrange(1000)]
100 loops, best of 3: 4.82 ms per loop
sage: import numpy
sage: %timeit [numpy.random.random_integers(0,10) for i in xrange(1000)]
1000 loops, best of 3: 1.5 ms per loop
sage: %timeit numpy.random.random_integers(0,10,1000)
10000 loops, best of 3: 53.7 µs per loop
Fri, 11 Jul 2014 20:29:24 +0200https://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/?answer=23379#post-id-23379Comment by Anschel Schaffer-Cohen for <p>Sage includes <code>numpy</code>, you can compare the different timings: </p>
<pre><code>sage: %timeit [random.randint(0,10) for i in xrange(1000)]
100 loops, best of 3: 5.61 ms per loop
sage: %timeit [random.randrange(0,11) for i in xrange(1000)]
100 loops, best of 3: 4.82 ms per loop
sage: import numpy
sage: %timeit [numpy.random.random_integers(0,10) for i in xrange(1000)]
1000 loops, best of 3: 1.5 ms per loop
sage: %timeit numpy.random.random_integers(0,10,1000)
10000 loops, best of 3: 53.7 µs per loop
</code></pre>
https://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/?comment=23395#post-id-23395Looks like the last of these is the fastest, thanks.Sun, 13 Jul 2014 05:56:09 +0200https://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/?comment=23395#post-id-23395Comment by god.one for <p>Sage includes <code>numpy</code>, you can compare the different timings: </p>
<pre><code>sage: %timeit [random.randint(0,10) for i in xrange(1000)]
100 loops, best of 3: 5.61 ms per loop
sage: %timeit [random.randrange(0,11) for i in xrange(1000)]
100 loops, best of 3: 4.82 ms per loop
sage: import numpy
sage: %timeit [numpy.random.random_integers(0,10) for i in xrange(1000)]
1000 loops, best of 3: 1.5 ms per loop
sage: %timeit numpy.random.random_integers(0,10,1000)
10000 loops, best of 3: 53.7 µs per loop
</code></pre>
https://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/?comment=23411#post-id-23411Hi, I got a question unrelated to this one but to your code. If I stupidly copy and paste it in a sagenb.org notebook, it throws different errors like 'invalid syntax'. The version that works for me is
timeit('[randint(0,10) for i in xrange(1000)]')
timeit('[randrange(0,11) for i in xrange(1000)]')
import numpy
timeit('[numpy.random.random_integers(0,10) for i in xrange(1000)]')
timeit('numpy.random.random_integers(0,10,1000)')
Can someone explain this to me or give me a hint for which keywords I have to search? Thanks in advance.Mon, 14 Jul 2014 06:55:07 +0200https://ask.sagemath.org/question/23377/fast-uniform-random-integer-generation/?comment=23411#post-id-23411