ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 05 Feb 2018 03:09:07 -0600Uniform random choice of integerhttp://ask.sagemath.org/question/40960/uniform-random-choice-of-integer/I want to perform some statistical sampling and to do this I need to uniformly randomly choose an integer from [0, N) where $N \approx 10^{50}$. It appears that there are several plausible ways to do this in Sage:
1) randint(0, N-1)
However, the standard Python random library appears to have some non-uniformity, for example [see this ticket](https://bugs.python.org/issue9025).
2) import numpy; numpy.random.randint(0, N)
However, since N is so large, this raises
ValueError: high is out of bounds for int64
3) ZZ.random_element(0, N)
> Do either of the issues that methods 1) and 2) suffer from apply to method 3)? That is, is 3) the correct way to integers uniformly at random?Sat, 03 Feb 2018 12:28:10 -0600http://ask.sagemath.org/question/40960/uniform-random-choice-of-integer/Answer by Emmanuel Charpentier for <p>I want to perform some statistical sampling and to do this I need to uniformly randomly choose an integer from [0, N) where $N \approx 10^{50}$. It appears that there are several plausible ways to do this in Sage:</p>
<p>1) randint(0, N-1)</p>
<p>However, the standard Python random library appears to have some non-uniformity, for example <a href="https://bugs.python.org/issue9025">see this ticket</a>.</p>
<p>2) import numpy; numpy.random.randint(0, N)</p>
<p>However, since N is so large, this raises</p>
<pre><code>ValueError: high is out of bounds for int64
</code></pre>
<p>3) ZZ.random_element(0, N)</p>
<blockquote>
<p>Do either of the issues that methods 1) and 2) suffer from apply to method 3)? That is, is 3) the correct way to integers uniformly at random?</p>
</blockquote>
http://ask.sagemath.org/question/40960/uniform-random-choice-of-integer/?answer=40978#post-id-40978Maybe, this :
sage: N=1e50
sage: L=2000
sage: foo=[Integer(t) for t in r.round(r.runif(L,0,N)).sage()]
would have better properties ?
Mon, 05 Feb 2018 03:09:07 -0600http://ask.sagemath.org/question/40960/uniform-random-choice-of-integer/?answer=40978#post-id-40978Answer by tmonteil for <p>I want to perform some statistical sampling and to do this I need to uniformly randomly choose an integer from [0, N) where $N \approx 10^{50}$. It appears that there are several plausible ways to do this in Sage:</p>
<p>1) randint(0, N-1)</p>
<p>However, the standard Python random library appears to have some non-uniformity, for example <a href="https://bugs.python.org/issue9025">see this ticket</a>.</p>
<p>2) import numpy; numpy.random.randint(0, N)</p>
<p>However, since N is so large, this raises</p>
<pre><code>ValueError: high is out of bounds for int64
</code></pre>
<p>3) ZZ.random_element(0, N)</p>
<blockquote>
<p>Do either of the issues that methods 1) and 2) suffer from apply to method 3)? That is, is 3) the correct way to integers uniformly at random?</p>
</blockquote>
http://ask.sagemath.org/question/40960/uniform-random-choice-of-integer/?answer=40961#post-id-40961At least, the method is different since `ZZ.random_element` use random generation from `gmp` (or its fork `mpir`), so it should not show the same drawbacks (though i can not guarantee that there is no other issue) . It is moreover much faster than `randint`.Sat, 03 Feb 2018 13:11:16 -0600http://ask.sagemath.org/question/40960/uniform-random-choice-of-integer/?answer=40961#post-id-40961Answer by dan_fulea for <p>I want to perform some statistical sampling and to do this I need to uniformly randomly choose an integer from [0, N) where $N \approx 10^{50}$. It appears that there are several plausible ways to do this in Sage:</p>
<p>1) randint(0, N-1)</p>
<p>However, the standard Python random library appears to have some non-uniformity, for example <a href="https://bugs.python.org/issue9025">see this ticket</a>.</p>
<p>2) import numpy; numpy.random.randint(0, N)</p>
<p>However, since N is so large, this raises</p>
<pre><code>ValueError: high is out of bounds for int64
</code></pre>
<p>3) ZZ.random_element(0, N)</p>
<blockquote>
<p>Do either of the issues that methods 1) and 2) suffer from apply to method 3)? That is, is 3) the correct way to integers uniformly at random?</p>
</blockquote>
http://ask.sagemath.org/question/40960/uniform-random-choice-of-integer/?answer=40966#post-id-40966Here is an other possibility:
sage: import random
sage: random.getrandbits(167)
96541868459945240442714629364038017535847363449131L
sage: ZZ(_)
96541868459945240442714629364038017535847363449131
sage: len(str(_))
50
(Well, $51$ decimal digits will also occur... I hope this is not an issue in the further application.)Sat, 03 Feb 2018 15:18:39 -0600http://ask.sagemath.org/question/40960/uniform-random-choice-of-integer/?answer=40966#post-id-40966