Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Air is water with holes in it.


devel / comp.lang.python / random.SystemRandom().randint() inefficient

SubjectAuthor
* random.SystemRandom().randint() inefficientCecil Westerhof
+* Re: random.SystemRandom().randint() inefficientStefan Ram
|`- Re: random.SystemRandom().randint() inefficientWeatherby,Gerard
+* Re: random.SystemRandom().randint() inefficientChris Angelico
|`* Re: random.SystemRandom().randint() inefficientCecil Westerhof
| `* Re: random.SystemRandom().randint() inefficientChris Angelico
|  `* Re: random.SystemRandom().randint() inefficientCecil Westerhof
|   +* Re: random.SystemRandom().randint() inefficientChris Angelico
|   |`- Re: random.SystemRandom().randint() inefficientCecil Westerhof
|   +* Re: random.SystemRandom().randint() inefficientDennis Lee Bieber
|   |`- Re: random.SystemRandom().randint() inefficientCecil Westerhof
|   `* Re: random.SystemRandom().randint() inefficientMichael F. Stemper
|    `* Re: random.SystemRandom().randint() inefficientCecil Westerhof
|     +* Re: random.SystemRandom().randint() inefficientMRAB
|     |`* Re: random.SystemRandom().randint() inefficientCecil Westerhof
|     | `* Re: random.SystemRandom().randint() inefficientMRAB
|     |  `- Re: random.SystemRandom().randint() inefficientCecil Westerhof
|     +* Re: random.SystemRandom().randint() inefficientAlan Bawden
|     |`* Re: random.SystemRandom().randint() inefficientCecil Westerhof
|     | `- Re: random.SystemRandom().randint() inefficientAlan Bawden
|     `* Re: random.SystemRandom().randint() inefficientRoel Schroeven
|      `* Re: random.SystemRandom().randint() inefficientCecil Westerhof
|       `- Re: random.SystemRandom().randint() inefficientChris Angelico
+* Re: random.SystemRandom().randint() inefficientDennis Lee Bieber
|`* Re: random.SystemRandom().randint() inefficientChris Angelico
| `- Re: random.SystemRandom().randint() inefficientCecil Westerhof
`* Re: random.SystemRandom().randint() inefficientBarry
 `* Re: random.SystemRandom().randint() inefficientCecil Westerhof
  +* Re: random.SystemRandom().randint() inefficientDennis Lee Bieber
  |`- Re: random.SystemRandom().randint() inefficientCecil Westerhof
  +- Re: random.SystemRandom().randint() inefficientMats Wichmann
  `- Re: random.SystemRandom().randint() inefficientBarry

Pages:12
random.SystemRandom().randint() inefficient

<87pmhs7xch.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23368&group=comp.lang.python#23368

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: random.SystemRandom().randint() inefficient
Date: Tue, 26 Jul 2022 16:38:38 +0200
Organization: Decebal Computing
Lines: 16
Message-ID: <87pmhs7xch.fsf@munus.decebal.nl>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="92878536266b0c1ae379650b12581eb6";
logging-data="2136249"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18DNj5ufb7kHrTH1t0F7CT1X9V0PUXY7Ck="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:+a1NGahaFK/iI9/b3Vx0S5FG4Yg=
sha1:jpxTXoFsw2bdunbvN3Nn9UC8sg0=
 by: Cecil Westerhof - Tue, 26 Jul 2022 14:38 UTC

I need to get a random integer. At first I tried it with:
from secrets import randbelow
index = randbelow(len(to_try))

This works perfectly, but it took some time. So I thought I try:
from random import SystemRandom
index = SystemRandom().randint(0, len(to_try) - 1)

A first indication is that the second version would take about two
times as much time as the first. Is there a reason for this, or should
this not be happening?

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: random.SystemRandom().randint() inefficient

<randomness-20220726163029@ram.dialup.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23370&group=comp.lang.python#23370

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: 26 Jul 2022 15:33:46 GMT
Organization: Stefan Ram
Lines: 14
Expires: 1 Apr 2023 11:59:58 GMT
Message-ID: <randomness-20220726163029@ram.dialup.fu-berlin.de>
References: <87pmhs7xch.fsf@munus.decebal.nl>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de x/w+kM6vk+yinV83giqezA1pi6nRVAfMlFf1rj0B3Aja0v
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Tue, 26 Jul 2022 15:33 UTC

Cecil Westerhof <Cecil@decebal.nl> writes:
>I need to get a random integer.

There are all kinds of trade-offs. When you need entropy,
the function sometimes has to collect data from hardware,
which takes some time. Pseudo-random integers sometimes can
be calculated faster. As a compromise, you can get some
entropy and use it to initialize ("seed") a pseudo-random
number generator and then get some pseudo-random numbers
until you seed it again. A linear-congruential generator
should be fast enough, but in some cases might not be random
enough.

Re: random.SystemRandom().randint() inefficient

<mailman.95.1658858104.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23371&group=comp.lang.python#23371

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: gweatherby@uchc.edu (Weatherby,Gerard)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Tue, 26 Jul 2022 17:30:32 +0000
Lines: 26
Message-ID: <mailman.95.1658858104.20444.python-list@python.org>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<randomness-20220726163029@ram.dialup.fu-berlin.de>
<5c3c316b-2032-4a6a-8390-786d8ce04c71@Spark>
Mime-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
X-Trace: news.uni-berlin.de CzfqyMMm2FAZPhBNVdOEggwHILqDvvhKW1uInIKP9ZJw==
Return-Path: <prvs=0206ea6a35=gweatherby@uchc.edu>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=uchc.edu header.i=@uchc.edu header.b=JPluud1d;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.022
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'jul': 0.04; 'random':
0.05; '26,': 0.07; 'message-id:@Spark': 0.07; 'ram': 0.07; '().':
0.09; 'approaches': 0.09; 'enough.': 0.09; 'writes:': 0.09;
'url:mailman': 0.15; '***': 0.16; '2022,': 0.16; 'api.': 0.16;
'email name:&lt;ram': 0.16; 'enough,': 0.16; 'initialize': 0.16;
'subject:() ': 0.16; 'url:urldefense': 0.16; 'url:v3': 0.16;
'wrote:': 0.16; 'to:addr:python-list': 0.20; '\xe2\x80\x94': 0.22;
'to:name:python-list@python.org': 0.24; 'url:listinfo': 0.25;
'opening': 0.26; 'received:edu': 0.26; 'stefan': 0.26; 'task':
0.26; 'function': 0.27; 'takes': 0.31; 'but': 0.32; 'there': 0.33;
'header:In-Reply-To:1': 0.34; 'cases': 0.36; 'received:filterd':
0.37; 'received:pps.filterd': 0.37; 'use': 0.39; 'something':
0.40; 'want': 0.40; 'should': 0.40; 'email.': 0.61; 'follow':
0.62; 'true': 0.63; 'health': 0.63; 'number,': 0.64; 'time.':
0.66; 'numbers': 0.67; 'header:Received:6': 0.67; 'collect': 0.69;
'generator': 0.69; 'url-ip:52.6/16': 0.69; 'url:xhtml': 0.74;
'url:1999': 0.75; 'clicking': 0.76; 'links.': 0.81; 'url:api':
0.84; '11:45': 0.84; 'attention:': 0.84; 'biology': 0.84; 'nan':
0.84; 'uconn': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uchc.edu;
h=from : to : subject :
date : message-id : references : in-reply-to : content-type :
mime-version; s=UCHC03162020;
bh=JF6gRl30RoSGcz1xknFYG6Uwk+QPg84kzqVDH8UMxAI=;
b=JPluud1dyoFylJIGKAmOqMWTLCdzR3ihLayYY18Unct4kwVHJymnEvBajAL7tTSJFFLf
cW9agd2wzUgcmAGVQYsBrcEdx2t14abDK7OZWFF1aauITuJSbQl/3OLfg9Hbb9V4b6RZ
ByW7IpBbMUoK59NMqy0TCfenRhaH4PWf3MfJfjylpF94OwZV3ajJgC+lr3IFFOCDqfXa
rJIg2h57RhxCkYgsMdePceOl3ibks5B/DnM7G/RA4KmeczLxnpEHeeTN094ryJFqSe4Y
N6017oJnGvW0T8OaM+mQuyRUVsCTiPUAprb0eWFGIyBq7QDPNMIdlX/55Z9Fqv8kPg6a RA==
Thread-Topic: random.SystemRandom().randint() inefficient
Thread-Index: AQHYoQPf3KW6KX309k21HrMhC0Jrcq2Qy2OkgABgZwA=
In-Reply-To: <randomness-20220726163029@ram.dialup.fu-berlin.de>
Accept-Language: en-US
Content-Language: en-US
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
x-ms-exchange-messagesentrepresentingtype: 1
x-ms-exchange-transport-fromentityheader: Hosted
x-originating-ip: [173.72.171.176]
X-Proofpoint-ORIG-GUID: jmTI0fYjaJPGBxVASXbgyuP_hjcBjvW_
X-Proofpoint-GUID: jmTI0fYjaJPGBxVASXbgyuP_hjcBjvW_
X-Proofpoint-Virus-Version: vendor=baseguard
engine=ICAP:2.0.205,Aquarius:18.0.883,Hydra:6.0.517,FMLib:17.11.122.1
definitions=2022-07-26_05,2022-07-26_01,2022-06-22_01
X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0
phishscore=0 spamscore=0
bulkscore=0 mlxscore=0 priorityscore=1501 suspectscore=0 clxscore=1015
impostorscore=0 adultscore=0 mlxlogscore=571 malwarescore=0
lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1
engine=8.12.0-2206140000 definitions=main-2207260067
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <5c3c316b-2032-4a6a-8390-786d8ce04c71@Spark>
X-Mailman-Original-References: <87pmhs7xch.fsf@munus.decebal.nl>
<randomness-20220726163029@ram.dialup.fu-berlin.de>
 by: Weatherby,Gerard - Tue, 26 Jul 2022 17:30 UTC

Absolutely. The task (“generate a random number”) is ill-defined.
Want a fast random number? Use 552015933 (I just retrieved it from random.org).
Want a true random number, use random.org API. (https://api.random.org/pricing).
Something in between, follow approaches Stefan suggests.


Gerard Weatherby | Application Architect NMRbox | NAN | Department of Molecular Biology and Biophysics
UConn Health 263 Farmington Avenue, Farmington, CT 06030-6406 uchc.edu
On Jul 26, 2022, 11:45 AM -0400, Stefan Ram <ram@zedat.fu-berlin.de>, wrote:
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***
Cecil Westerhof <Cecil@decebal.nl> writes:
I need to get a random integer.
There are all kinds of trade-offs. When you need entropy,
the function sometimes has to collect data from hardware,
which takes some time. Pseudo-random integers sometimes can
be calculated faster. As a compromise, you can get some
entropy and use it to initialize ("seed") a pseudo-random
number generator and then get some pseudo-random numbers
until you seed it again. A linear-congruential generator
should be fast enough, but in some cases might not be random
enough.

--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!kjJ7U6kkSW8uOTWJCnHeXn0Acczd619asmIrVMA_NOqvP_JMqqQ1Rpp61sptQucbOuEFGb6ot499_FqaRajiS9s$

Re: random.SystemRandom().randint() inefficient

<mailman.96.1658858493.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23372&group=comp.lang.python#23372

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: rosuav@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 04:01:20 +1000
Lines: 21
Message-ID: <mailman.96.1658858493.20444.python-list@python.org>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de b/B6tEcVEy9cyTJrfh/9fQpv2O5svm+Wb8tNzeZbLEwg==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=quhxh5r7;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.020
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'jul': 0.04; '2022': 0.05;
'random': 0.05; 'import': 0.15; 'chrisa': 0.16; 'comparison,':
0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'instance': 0.16; 'received:209.85.218': 0.16; 'subject:() ':
0.16; 'wrote:': 0.16; 'to:addr:python-list': 0.20; 'version':
0.23; 'tried': 0.26; 'python-list': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'there': 0.33; 'header:In-
Reply-To:1': 0.34; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; 'received:209.85': 0.37;
'received:209': 0.39; 'two': 0.39; 'single': 0.39; 'this,': 0.39;
'setting': 0.39; 'wed,': 0.39; 'should': 0.40; 'skip:r 20': 0.64;
'number,': 0.64; 'with:': 0.64; 'time.': 0.66; 'times': 0.69;
'took': 0.69; 'number.': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=gUzNdVY9ZgKT/hM8QXUXvqTR3OsICOc4UNKkBd73FRw=;
b=quhxh5r7Y6xHJa74nY0zdOceTKs1Wz7djnceI6kOxyLJYOfO00+j8Y0quh2An++wp5
Nkl6+bh0dhh14YrwGMUn2byt8h+++KLxtx03G0mUbfOXnVH31W7qY+EmILBIQ7wLEjbG
PglaoN/+fUB8SCVKD5j4avRjOTauyPrfPASfivSWEFoQzxVuo4rs6RXX9CX10MFwMhO/
0D8ez9jVQBv1ciEO+nWm5sKjJIElUy86oXXZvjw5J/mJjMT4py++nru5o3MlTSkQAjyV
wDvzuI2tHhurc2xMeaZgA8HcaniTBK2MqAGsMoYVBhJXZmBDNXP2Xi9ITlvVq8kZpZXc
VuKw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=gUzNdVY9ZgKT/hM8QXUXvqTR3OsICOc4UNKkBd73FRw=;
b=Uarxp/aWv7B26xg92ja2cv4tDJeX1XkC0xRWQbCzcWLjIpAYopQ/TJjQhPzqXbZ6kF
Aigm5EaMKYvtXaE7zzyuhFfjeRCJfoITNohvKfdBBWbwhmJEp0KeFYRlLhYS/f1yOGmy
6vg4cF2z7g+PzNci13hWCPDSzkhZNXlDNCoGcl51fa4KUuQZ83SS+rAePHYATgwSrWZU
g1Ivp8G0KvPYpcUuBt/z1hx61PMBN2DFXiax8SGKNCnYjvnk33FW8Gv7+IxEOFg3aUn1
UO7pWzueMjDiX2C5FsfmZcULE6ZBbWfaGAwuke62rdxd3htlXtwfh2FYJENK15uEv+w7
ks2Q==
X-Gm-Message-State: AJIora/W7jqg9xRyicNM5XKIXSCizZvO6qmxI2WEdxKpBkkI6Oj3GoGr
nXEmLWG7TlBRwom+pKqrCstRlTZOJnnafAr3xe3qBcJv
X-Google-Smtp-Source: AGRyM1u901ISUN8IXrdlo+jFNqaLzg7UYUkA3TMTDFerdTedeLUFqyjDrKuJ/SU3/I4BqjIkToq9o7LhM8OLhtJcREk=
X-Received: by 2002:a17:907:3d89:b0:72f:18b7:9035 with SMTP id
he9-20020a1709073d8900b0072f18b79035mr15630244ejc.13.1658858491990; Tue, 26
Jul 2022 11:01:31 -0700 (PDT)
In-Reply-To: <87pmhs7xch.fsf@munus.decebal.nl>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
X-Mailman-Original-References: <87pmhs7xch.fsf@munus.decebal.nl>
 by: Chris Angelico - Tue, 26 Jul 2022 18:01 UTC

On Wed, 27 Jul 2022 at 01:06, Cecil Westerhof via Python-list
<python-list@python.org> wrote:
>
> I need to get a random integer. At first I tried it with:
> from secrets import randbelow
> index = randbelow(len(to_try))
>
> This works perfectly, but it took some time. So I thought I try:
> from random import SystemRandom
> index = SystemRandom().randint(0, len(to_try) - 1)
>
> A first indication is that the second version would take about two
> times as much time as the first. Is there a reason for this, or should
> this not be happening?
>

You're setting up a brand new SystemRandom instance just for a single
random number. For a fairer comparison, set up the instance, then
generate far more than just a single number, and see how that goes.

ChrisA

Re: random.SystemRandom().randint() inefficient

<87lesf8ylo.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23376&group=comp.lang.python#23376

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Tue, 26 Jul 2022 21:26:11 +0200
Organization: Decebal Computing
Lines: 33
Message-ID: <87lesf8ylo.fsf@munus.decebal.nl>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="92878536266b0c1ae379650b12581eb6";
logging-data="2274760"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19WzfRKFTJpsbvSFAtFV8wM+9u0DZ8RpBY="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:1Y7Jb4nPMi9NiETaWSbXZYdP+H0=
sha1:q9CyUiZCpntiP9SWwvXpJhY0UQM=
 by: Cecil Westerhof - Tue, 26 Jul 2022 19:26 UTC

Chris Angelico <rosuav@gmail.com> writes:

> On Wed, 27 Jul 2022 at 01:06, Cecil Westerhof via Python-list
> <python-list@python.org> wrote:
>>
>> I need to get a random integer. At first I tried it with:
>> from secrets import randbelow
>> index = randbelow(len(to_try))
>>
>> This works perfectly, but it took some time. So I thought I try:
>> from random import SystemRandom
>> index = SystemRandom().randint(0, len(to_try) - 1)
>>
>> A first indication is that the second version would take about two
>> times as much time as the first. Is there a reason for this, or should
>> this not be happening?
>>
>
> You're setting up a brand new SystemRandom instance just for a single
> random number. For a fairer comparison, set up the instance, then
> generate far more than just a single number, and see how that goes.

Thanks. I thought I did something wrong and I did.
I will try to implement like you said and look what the result will
be. (And share it.)

(As I understand it both do more, or less the same and should have
comparable performance.)

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: random.SystemRandom().randint() inefficient

<mailman.99.1658866418.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23377&group=comp.lang.python#23377

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: rosuav@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 06:13:24 +1000
Lines: 45
Message-ID: <mailman.99.1658866418.20444.python-list@python.org>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de tfUlpXYXpfSlyP8pDAhN5gY1oXkYLjCNq24CspZf/7Rw==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=bOiovaDA;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.065
X-Spam-Evidence: '*H*': 0.87; '*S*': 0.00; 'jul': 0.04; '2022': 0.05;
'random': 0.05; 'suggestion': 0.07; 'angelico': 0.09; 'overhead':
0.09; 'writes:': 0.09; 'import': 0.15; 'chrisa': 0.16;
'comparison,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris
angelico': 0.16; 'instance': 0.16; 'subject:() ': 0.16; 'wrote:':
0.16; 'implement': 0.19; 'to:addr:python-list': 0.20; 'version':
0.23; 'thanks!': 0.24; '(and': 0.25; 'tried': 0.26; 'object':
0.26; 'chris': 0.28; 'wrong': 0.28; 'seem': 0.31; 'takes': 0.31;
"doesn't": 0.32; '(as': 0.32; 'python-list': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'there': 0.33; 'same':
0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; "it's": 0.37; 'received:209.85':
0.37; 'class': 0.37; 'hard': 0.37; 'could': 0.38; 'received:209':
0.39; 'two': 0.39; 'single': 0.39; 'this,': 0.39;
'received:209.85.208': 0.39; 'setting': 0.39; 'wed,': 0.39;
'both': 0.40; 'something': 0.40; 'try': 0.40; 'should': 0.40;
'feel': 0.63; 'share': 0.63; 'skip:r 20': 0.64; 'number,': 0.64;
'with:': 0.64; 'your': 0.64; 'look': 0.65; 'less': 0.65; 'time.':
0.66; 'more,': 0.67; 'times': 0.69; 'took': 0.69; 'interesting':
0.71; 'production': 0.71; 'performance': 0.71; 'yes': 0.76;
'significant': 0.78; 'comparable': 0.84; 'irrelevant,': 0.84;
'say,': 0.84; 'number.': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=VT7hc2cUdRKjW6Gvax+nGARbOsAHr0JOaPu1q1EdA7E=;
b=bOiovaDAIIpZkR617C8btCbdoT2tSc9Z/Xl5wFZlDDqvGK+6y6eARVqFH2D1GeoTQE
6861TLwRnaTTLoSRjfVUR6d8a41yktd668jqeG9rDwOOiyoldYuWTutCgLUvyMGnsbTz
ysOcwfP8M9jY6LMy3mjlECD/vFryBMCJbAL+y98uWsdJtZNvezhsh9FA7Hrcku1/antQ
9JM/8jI0CojcEx8h+HUXsu68STwmWsPkyi6MHATn23K168mQ/KxWGl0cnzZGeWGEJdWx
4uPOF5guHslIkjMajydKuG875T+O8/pmko5kZlTPtj1FRoDaQ81rGP20mWDXk//aRPhi
ZxaA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=VT7hc2cUdRKjW6Gvax+nGARbOsAHr0JOaPu1q1EdA7E=;
b=GYKZPau5Kt8zY2zxiSzKDlQ7DHjE3gPV9xbahNLF7zDY7ISkGkW82h9qRNPNKfhfs1
uCft1jOXTXI+/o/vRLQKI4uwsqL68jBN636lzPb5sJ2/ybpwP/VM9ztxImqDga8oYnug
jQ7QoGWmPcZ2FLMpheRWFKgCI8ibNfZ/O1/QUWzOYwbWO3o0LZvFcl3WBDUojOpRXgRB
tGY0DuICUnuRYAH4nPp7xosoEnZncJX5UTlfkNrc2mDVznAro2ASRwwOPRXx+oPSVHmK
gS2VyDAYmywgK/6TCgl6XUrbzMKqyvT8u1VbZNxFX79MlwUKHNPQeBykPAQB0PtyCE2L
VO1g==
X-Gm-Message-State: AJIora9pw/HC5Kv1ePNgQlpIhAzp/vTKIuGzLP5sAaXzuEkD+98S5NE/
EQPneZO8YbiLF2f2mlFsG0SguMoS7W4OV7cJ/R/TKfVA
X-Google-Smtp-Source: AGRyM1tAnbQaClq/IcN/5De8noWJtYTPhdo+4gZM9Nb1Hj8suHFAKTiPKBp4c7V/bYoLMniHluFUZDZ4KEonaHnNFFE=
X-Received: by 2002:aa7:cd6e:0:b0:43b:a7ba:e646 with SMTP id
ca14-20020aa7cd6e000000b0043ba7bae646mr19596659edb.231.1658866415829; Tue, 26
Jul 2022 13:13:35 -0700 (PDT)
In-Reply-To: <87lesf8ylo.fsf@munus.decebal.nl>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
X-Mailman-Original-References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
 by: Chris Angelico - Tue, 26 Jul 2022 20:13 UTC

On Wed, 27 Jul 2022 at 06:06, Cecil Westerhof via Python-list
<python-list@python.org> wrote:
>
> Chris Angelico <rosuav@gmail.com> writes:
>
> > On Wed, 27 Jul 2022 at 01:06, Cecil Westerhof via Python-list
> > <python-list@python.org> wrote:
> >>
> >> I need to get a random integer. At first I tried it with:
> >> from secrets import randbelow
> >> index = randbelow(len(to_try))
> >>
> >> This works perfectly, but it took some time. So I thought I try:
> >> from random import SystemRandom
> >> index = SystemRandom().randint(0, len(to_try) - 1)
> >>
> >> A first indication is that the second version would take about two
> >> times as much time as the first. Is there a reason for this, or should
> >> this not be happening?
> >>
> >
> > You're setting up a brand new SystemRandom instance just for a single
> > random number. For a fairer comparison, set up the instance, then
> > generate far more than just a single number, and see how that goes.
>
> Thanks. I thought I did something wrong and I did.
> I will try to implement like you said and look what the result will
> be. (And share it.)

Thanks! Don't feel bad; performance testing is *hard*, getting
meaningful results takes a lot of of fiddling with parameters, and
getting interesting AND meaningful results can sometimes seem about
impossible.

> (As I understand it both do more, or less the same and should have
> comparable performance.)

In normal production work? Yes (the SystemRandom object doesn't have
any significant state - a seeded RNG could have a lot more overhead
here). But for performance testing? The work of instantiating the
class could be completely irrelevant, or it could be dominating your
results. It's hard to say, hence the suggestion to try it without
reinstantiating.

ChrisA

Re: random.SystemRandom().randint() inefficient

<87h7338s1c.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23378&group=comp.lang.python#23378

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Tue, 26 Jul 2022 23:47:59 +0200
Organization: Decebal Computing
Lines: 71
Message-ID: <87h7338s1c.fsf@munus.decebal.nl>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
<mailman.99.1658866418.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="92878536266b0c1ae379650b12581eb6";
logging-data="2346564"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18lMKWXoxGcX1T/6qyGQ+NipGK0ESfw82Q="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:pm855gJ8a8iv1Pd+oCeiErAHXAU=
sha1:+pNzRJVFwQOrhbO/lXEi4oduBbg=
 by: Cecil Westerhof - Tue, 26 Jul 2022 21:47 UTC

Chris Angelico <rosuav@gmail.com> writes:

> On Wed, 27 Jul 2022 at 06:06, Cecil Westerhof via Python-list
> <python-list@python.org> wrote:
>>
>> Chris Angelico <rosuav@gmail.com> writes:
>>
>> > On Wed, 27 Jul 2022 at 01:06, Cecil Westerhof via Python-list
>> > <python-list@python.org> wrote:
>> >>
>> >> I need to get a random integer. At first I tried it with:
>> >> from secrets import randbelow
>> >> index = randbelow(len(to_try))
>> >>
>> >> This works perfectly, but it took some time. So I thought I try:
>> >> from random import SystemRandom
>> >> index = SystemRandom().randint(0, len(to_try) - 1)
>> >>
>> >> A first indication is that the second version would take about two
>> >> times as much time as the first. Is there a reason for this, or should
>> >> this not be happening?
>> >>
>> >
>> > You're setting up a brand new SystemRandom instance just for a single
>> > random number. For a fairer comparison, set up the instance, then
>> > generate far more than just a single number, and see how that goes.
>>
>> Thanks. I thought I did something wrong and I did.
>> I will try to implement like you said and look what the result will
>> be. (And share it.)
>
> Thanks! Don't feel bad; performance testing is *hard*, getting
> meaningful results takes a lot of of fiddling with parameters, and
> getting interesting AND meaningful results can sometimes seem about
> impossible.
>
>> (As I understand it both do more, or less the same and should have
>> comparable performance.)
>
> In normal production work? Yes (the SystemRandom object doesn't have
> any significant state - a seeded RNG could have a lot more overhead
> here). But for performance testing? The work of instantiating the
> class could be completely irrelevant, or it could be dominating your
> results. It's hard to say, hence the suggestion to try it without
> reinstantiating.

It had a very big influence. Original it took about three times more
time to run my program. (The program was still running when I posted
the original post and the difference was higher as I anticipated.)
Removing that did cut about 45% of the execution time of the program.
(So the initiation is quit expensive.)
But it still takes about 50% more time. So I am still a bit
flabbergasted.

The new code:
from random import SystemRandom
system_random = SystemRandom()
index = system_random.randint(0, len(to_try) - 1)

The first two statements are executed once.
The last statement I think about 75 * 10 ** 6.

So it seems that my first idea of using randbelow was the correct one.
But if anyone could explain why SystemRandom is so much more
expensive, I would be interested to know it.
(Or am I still doing something wrong?)

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: random.SystemRandom().randint() inefficient

<mailman.100.1658875454.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23379&group=comp.lang.python#23379

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: rosuav@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 08:44:01 +1000
Lines: 129
Message-ID: <mailman.100.1658875454.20444.python-list@python.org>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
<mailman.99.1658866418.20444.python-list@python.org>
<87h7338s1c.fsf@munus.decebal.nl>
<CAPTjJmpPA57iF3d6nRHgzPtdwW_VpcQSHGe_NK5qau0FJGnnGg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de mDGj6kFxhjzGK8w8wvy9vgG5CvuL/IxOURs/xYoCuxtA==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=MYJqkL0t;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.048
X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'jul': 0.04; '2022': 0.05;
'random': 0.05; 'loop': 0.07; 'suggestion': 0.07; 'angelico':
0.09; 'overhead': 0.09; 'writes:': 0.09; 'import': 0.15; '(3)':
0.16; '(so': 0.16; '45%': 0.16; 'chrisa': 0.16; 'comparison,':
0.16; 'endpoint': 0.16; 'executed': 0.16; 'from:addr:rosuav':
0.16; 'from:name:chris angelico': 0.16; 'incremented': 0.16;
'instance': 0.16; 'loops,': 0.16; 'object,': 0.16; 'once.': 0.16;
'python3': 0.16; 'received:209.85.218': 0.16; 'similar,': 0.16;
'subject:() ': 0.16; 'wrote:': 0.16; 'probably': 0.17;
'implement': 0.19; 'to:addr:python-list': 0.20; 'bit.': 0.22;
"what's": 0.22; 'version': 0.23; 'run': 0.23; 'thanks!': 0.24;
'idea': 0.24; '(and': 0.25; 'actual': 0.25; 'anyone': 0.25;
'seems': 0.26; 'tried': 0.26; 'object': 0.26; "isn't": 0.27;
'bit': 0.27; 'chris': 0.28; 'wrong': 0.28; 'code,': 0.31; 'seem':
0.31; 'takes': 0.31; 'program': 0.31; 'think': 0.32; "doesn't":
0.32; '(as': 0.32; 'amounts': 0.32; 'execution': 0.32; 'python-
list': 0.32; 'transform': 0.32; 'zero': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'there': 0.33; 'path':
0.33; 'able': 0.34; 'same': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'running': 0.34; 'one.': 0.35;
'from:addr:gmail.com': 0.35; 'work,': 0.36; 'source': 0.36;
'couple': 0.37; "skip:' 10": 0.37; 'using': 0.37; "it's": 0.37;
'received:209.85': 0.37; 'class': 0.37; 'hard': 0.37; 'could':
0.38; 'received:209': 0.39; 'two': 0.39; 'added': 0.39; 'adding':
0.39; 'single': 0.39; 'this,': 0.39; 'setting': 0.39; 'use': 0.39;
'wed,': 0.39; 'still': 0.40; 'explain': 0.40; 'happen': 0.40;
'program.': 0.40; 'both': 0.40; 'something': 0.40; 'try': 0.40;
'should': 0.40; 'higher': 0.60; 'best': 0.61; 'method': 0.61;
'here.': 0.61; 'skip:i 20': 0.62; 'internal': 0.63; 'feel': 0.63;
'share': 0.63; 'skip:r 20': 0.64; 'number,': 0.64; 'with:': 0.64;
'your': 0.64; 'look': 0.65; 'less': 0.65; 'time.': 0.66; 'more,':
0.67; 'per': 0.68; 'interested': 0.68; 'times': 0.69; 'took':
0.69; 'instead,': 0.70; 'interesting': 0.71; 'production': 0.71;
'performance': 0.71; 'yes': 0.76; 'significant': 0.78; 'extra':
0.84; 'happens': 0.84; '200000': 0.84; 'attribute': 0.84;
'comparable': 0.84; 'evaporate.': 0.84; 'irrelevant,': 0.84;
'say,': 0.84; 'tiny': 0.84; 'avoiding': 0.91; 'number.': 0.91;
'cut': 0.95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=d9hUqLGtcmq9GwvCOeIN4bTFS1vnPOfYfjKfU3ID+pM=;
b=MYJqkL0tV1yHJZx8KbggeK74v9NEmjA4go3RdWlAbUb4lKjHmvkD+zIxT82BpngoR1
N3Q9I5DB4bj16nm5zvvMTrfeg39q9xOS1oy9Cm6Mhn9lBnurqYwU1rIfR4fpg8sFB1cx
NXetKDWLTh1Egn1Oqiih3pITCKIVc20k4lentRSTvN0dQjtzLlvQWzD9o7YU+l6J23OI
u5tROCDw0AexmaUXogy5OYMgn2//fP5B8g+XuF5QVCSitbexktxnd1VHhNfUZ3U8TayO
5FKKEUDYgri2fzGqUmhQB294xKc8ylID5WqDUm0yz7YBbwEpSpgB1TjF7ygD0Tde6UbA
FjCA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=d9hUqLGtcmq9GwvCOeIN4bTFS1vnPOfYfjKfU3ID+pM=;
b=LOIozJcP0EdctJQQ//7LNYRypa/AMbL5DuoATF4eh4ZvEbp1PIoXo5ZmOOkje764r/
VIbylqwIqc8wjD/C0ko7yvVMpwHsZt+40nsf24IzRbvpXnGCT4hkTbq11wHjTpomN6bs
r7NR5wyQOmuIGKU9UJzT0GRkEomhO8IXNkBanL6JAavvZFsmw74BfvJpFLkZtm+TYAOr
14HYVjaZx8CpCKB1Ho1OKBsTogLzAlMtqCky8iw5G4i4AFKGj4tDkYIXFdIE7lJtPwfK
GmTuU+ekICXu7wh7fuH7H8xVgsXLpizEK0pFiN3U5B5TVhYKKpAXus8VgAoj+L1B/Oqu
Bi8A==
X-Gm-Message-State: AJIora8NNRngOqhp7M2axuvuLV8ov//0jC+wyLN8MvG5gN2eNVhJiRTC
B5P2Jb+7bKTmiTjmXHagBIvuYxXwK8cnQNnes/Khcu/vkXQ=
X-Google-Smtp-Source: AGRyM1vhedgZTtGYYDSX+xDqYaRWXVc7QVb6k7AWE/KMbcrqsx62jYK305DOEq6esGRHezqCkZ44SVLpCEq1RV7ee94=
X-Received: by 2002:a17:907:72cb:b0:72b:820a:f09b with SMTP id
du11-20020a17090772cb00b0072b820af09bmr15378083ejc.335.1658875452213; Tue, 26
Jul 2022 15:44:12 -0700 (PDT)
In-Reply-To: <87h7338s1c.fsf@munus.decebal.nl>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAPTjJmpPA57iF3d6nRHgzPtdwW_VpcQSHGe_NK5qau0FJGnnGg@mail.gmail.com>
X-Mailman-Original-References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
<mailman.99.1658866418.20444.python-list@python.org>
<87h7338s1c.fsf@munus.decebal.nl>
 by: Chris Angelico - Tue, 26 Jul 2022 22:44 UTC

On Wed, 27 Jul 2022 at 08:18, Cecil Westerhof via Python-list
<python-list@python.org> wrote:
>
> Chris Angelico <rosuav@gmail.com> writes:
>
> > On Wed, 27 Jul 2022 at 06:06, Cecil Westerhof via Python-list
> > <python-list@python.org> wrote:
> >>
> >> Chris Angelico <rosuav@gmail.com> writes:
> >>
> >> > On Wed, 27 Jul 2022 at 01:06, Cecil Westerhof via Python-list
> >> > <python-list@python.org> wrote:
> >> >>
> >> >> I need to get a random integer. At first I tried it with:
> >> >> from secrets import randbelow
> >> >> index = randbelow(len(to_try))
> >> >>
> >> >> This works perfectly, but it took some time. So I thought I try:
> >> >> from random import SystemRandom
> >> >> index = SystemRandom().randint(0, len(to_try) - 1)
> >> >>
> >> >> A first indication is that the second version would take about two
> >> >> times as much time as the first. Is there a reason for this, or should
> >> >> this not be happening?
> >> >>
> >> >
> >> > You're setting up a brand new SystemRandom instance just for a single
> >> > random number. For a fairer comparison, set up the instance, then
> >> > generate far more than just a single number, and see how that goes.
> >>
> >> Thanks. I thought I did something wrong and I did.
> >> I will try to implement like you said and look what the result will
> >> be. (And share it.)
> >
> > Thanks! Don't feel bad; performance testing is *hard*, getting
> > meaningful results takes a lot of of fiddling with parameters, and
> > getting interesting AND meaningful results can sometimes seem about
> > impossible.
> >
> >> (As I understand it both do more, or less the same and should have
> >> comparable performance.)
> >
> > In normal production work? Yes (the SystemRandom object doesn't have
> > any significant state - a seeded RNG could have a lot more overhead
> > here). But for performance testing? The work of instantiating the
> > class could be completely irrelevant, or it could be dominating your
> > results. It's hard to say, hence the suggestion to try it without
> > reinstantiating.
>
> It had a very big influence. Original it took about three times more
> time to run my program. (The program was still running when I posted
> the original post and the difference was higher as I anticipated.)
> Removing that did cut about 45% of the execution time of the program.
> (So the initiation is quit expensive.)
> But it still takes about 50% more time. So I am still a bit
> flabbergasted.
>
> The new code:
> from random import SystemRandom
> system_random = SystemRandom()
> index = system_random.randint(0, len(to_try) - 1)
>
> The first two statements are executed once.
> The last statement I think about 75 * 10 ** 6.
>
> So it seems that my first idea of using randbelow was the correct one.
> But if anyone could explain why SystemRandom is so much more
> expensive, I would be interested to know it.
> (Or am I still doing something wrong?)

Hmm. There are still a lot of differences here. Are you able to make
use of randrange() instead, to make them more consistent?

According to the source code, secrets.randbelow is calling on an
internal method _randbelow of the SystemRandom object, but randrange
(if called with only one arg) will go straight into that same method.
Here's my results:

rosuav@sikorsky:~$ python3 -m timeit -s 'from random import randrange'
'randrange(10000)'
1000000 loops, best of 5: 322 nsec per loop
rosuav@sikorsky:~$ python3 -m timeit -s 'from random import
SystemRandom; r = SystemRandom()' 'r.randint(0, 10000)'
200000 loops, best of 5: 1.92 usec per loop
rosuav@sikorsky:~$ python3 -m timeit -s 'from random import
SystemRandom; r = SystemRandom()' 'r.randrange(10000)'
200000 loops, best of 5: 1.87 usec per loop
rosuav@sikorsky:~$ python3 -m timeit -s 'from secrets import
randbelow' 'randbelow(10000)'
200000 loops, best of 5: 1.64 usec per loop

(The difference with the first one is that it isn't using the system
RNG, so it has the limitations of an internal PRNG.)

When you call randint, what happens is (1) the endpoint is incremented
to transform it from inclusive-inclusive to inclusive-exclusive; (2)
randrange is called with two args; (3) fast path 1 is skipped, fast
path 2 is taken, and _randbelow gets called to get an actual random
number, which gets zero added to it before returning.

If, instead, you use randrange(len(to_try)), what would happen is (1)
fast path 1 is used, and (2) _randbelow is called to get the random
number.

In secrets.randbelow, it's even better: (1) _randbelow is called to
get the random number.

I think that might be what's going on here. You're adding some tiny
amounts of extra work, but if you were to make them more similar, the
distinctions would evaporate. Here's a couple of other variants that
use SystemRandom:

rosuav@sikorsky:~$ python3 -m timeit -s 'from random import
SystemRandom; randrange = SystemRandom().randrange' 'randrange(10000)'
200000 loops, best of 5: 1.81 usec per loop
rosuav@sikorsky:~$ python3 -m timeit -s 'from random import
SystemRandom; randrange = SystemRandom()._randbelow'
'randrange(10000)'
200000 loops, best of 5: 1.61 usec per loop

Note that we shave a few usec by avoiding the attribute lookup, but
even more by directly calling _randbelow, which is why
secrets.randbelow is able to save a bit. But don't use internal
methods; you can probably get pretty much equivalent performance from
randrange, which is public.

Hope that's of some value!

ChrisA

Re: random.SystemRandom().randint() inefficient

<kqr0eh9jaj6hps56r8ue5nls1an5dhgq1q@4ax.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23380&group=comp.lang.python#23380

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!border-1.nntp.ord.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!buffer1.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 26 Jul 2022 17:58:37 -0500
From: wlfraed@ix.netcom.com (Dennis Lee Bieber)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Tue, 26 Jul 2022 18:58:37 -0400
Organization: IISS Elusive Unicorn
Message-ID: <kqr0eh9jaj6hps56r8ue5nls1an5dhgq1q@4ax.com>
References: <87pmhs7xch.fsf@munus.decebal.nl>
User-Agent: ForteAgent/8.00.32.1272
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 37
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-ui6zCZjqjCbDT0TOWG8R4hEgc5xhGavpf0mBjgwTBhk/pHz+HKm/GDVD/2GAUqDbKoSl1ylZ3jzeCfL!rEmBck47vSrVQXz1vqcIYO29l27d6px+vfIZAOrQjWZOQCgpKgoYVtmv+vnaEAJs1pgqYS64
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 2351
X-Received-Bytes: 2473
 by: Dennis Lee Bieber - Tue, 26 Jul 2022 22:58 UTC

On Tue, 26 Jul 2022 16:38:38 +0200, Cecil Westerhof <Cecil@decebal.nl>
declaimed the following:

>I need to get a random integer. At first I tried it with:
> from secrets import randbelow
> index = randbelow(len(to_try))
>
>This works perfectly, but it took some time. So I thought I try:
> from random import SystemRandom
> index = SystemRandom().randint(0, len(to_try) - 1)
>
>A first indication is that the second version would take about two
>times as much time as the first. Is there a reason for this, or should
>this not be happening?

Well, off the top of my head...

For one generation of "index" you are first creating an instance of
SystemRandom(), using it to generate your random integer, and then
disposing of the instance.

If you only need ONE random integer, the time difference probably
doesn't matter. OTOH, if you need many during the run, using

sr = SystemRandom()
#stuff in some loop that generates multiple ints
index = sr.randint(...)

Hmmm, wonder if there is a speed difference between
.randint(0, len(to_try) - 1)
and
.randint(1, len(to_try)) - 1

--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

Re: random.SystemRandom().randint() inefficient

<dgs0ehps5h73vob431svjnt3d7pqmv8cds@4ax.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23381&group=comp.lang.python#23381

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!buffer1.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 26 Jul 2022 18:02:20 -0500
From: wlfraed@ix.netcom.com (Dennis Lee Bieber)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Tue, 26 Jul 2022 19:02:21 -0400
Organization: IISS Elusive Unicorn
Message-ID: <dgs0ehps5h73vob431svjnt3d7pqmv8cds@4ax.com>
References: <87pmhs7xch.fsf@munus.decebal.nl> <CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com> <mailman.96.1658858493.20444.python-list@python.org> <87lesf8ylo.fsf@munus.decebal.nl> <CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com> <mailman.99.1658866418.20444.python-list@python.org> <87h7338s1c.fsf@munus.decebal.nl>
User-Agent: ForteAgent/8.00.32.1272
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 29
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-wo4BF1raSM49gV/X+BimAiv1blyud9tl3AmyuXyKVpPigbCjLFRmtsC15yfkVo9QPaG8NT6947droxp!beHkj3CZgHI49RVRagcaZ8KHUXnDZO76/Qppi73dszf3ctMGlbdK/PDwaGQBcL2NZ/DQZocF
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 2278
 by: Dennis Lee Bieber - Tue, 26 Jul 2022 23:02 UTC

On Tue, 26 Jul 2022 23:47:59 +0200, Cecil Westerhof <Cecil@decebal.nl>
declaimed the following:

>The new code:
> from random import SystemRandom
> system_random = SystemRandom()
> index = system_random.randint(0, len(to_try) - 1)
>
>The first two statements are executed once.
>The last statement I think about 75 * 10 ** 6.
>
>So it seems that my first idea of using randbelow was the correct one.
>But if anyone could explain why SystemRandom is so much more
>expensive, I would be interested to know it.
>(Or am I still doing something wrong?)

What happens with

system_randint = SystemRandom().randint #no parens

index = system_randint(...)

which may remove the method lookup from the repetition.

--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

Re: random.SystemRandom().randint() inefficient

<mailman.101.1658879014.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23382&group=comp.lang.python#23382

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: rosuav@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 09:43:20 +1000
Lines: 45
Message-ID: <mailman.101.1658879014.20444.python-list@python.org>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<kqr0eh9jaj6hps56r8ue5nls1an5dhgq1q@4ax.com>
<CAPTjJmpVMTR-gabAa=YWLTgjQLRkaDtdRTvY+qYuZ-s8qovPoQ@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de 3nvIp9nsmcqpYxIOfsS9DAm9Qa5JikuZUsDdITnISvng==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=iLc1wqmy;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.010
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'jul': 0.04; '2022': 0.05;
'random': 0.05; 'loop': 0.07; 'help,': 0.14; 'import': 0.15;
'+0200,': 0.16; 'arithmetic': 0.16; 'chrisa': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'generates': 0.16; 'instance': 0.16; 'integer,': 0.16; 'item,':
0.16; 'received:209.85.208.42': 0.16; 'received:mail-
ed1-f42.google.com': 0.16; 'run,': 0.16; 'subject:() ': 0.16;
'wrote:': 0.16; 'probably': 0.17; 'tue,': 0.19; 'to:addr:python-
list': 0.20; 'way.': 0.22; 'version': 0.23; 'tried': 0.26;
'matter.': 0.26; 'creating': 0.27; 'done': 0.28; "doesn't": 0.32;
'message-id:@mail.gmail.com': 0.32; 'but': 0.32; 'there': 0.33;
'same': 0.34; 'header:In-Reply-To:1': 0.34; 'received:google.com':
0.34; 'trying': 0.35; 'definitely': 0.35; 'from:addr:gmail.com':
0.35; 'using': 0.37; 'received:209.85': 0.37; 'received:209':
0.39; 'two': 0.39; 'this,': 0.39; 'received:209.85.208': 0.39;
'wed,': 0.39; 'want': 0.40; 'should': 0.40; 'generation': 0.62;
'between': 0.63; 'skip:r 20': 0.64; 'with:': 0.64; 'your': 0.64;
'top': 0.65; 'well': 0.65; 'time.': 0.66; 'during': 0.69;
'following:': 0.69; 'took': 0.69; 'speed': 0.71; 'selecting':
0.76; 'significant,': 0.84; 'switching': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=Rqux/zW7xICXUIc9WnXIOH4l/PcDnNko21xXB1E3fws=;
b=iLc1wqmy1gnsepo5C090MxATKQ72oXXQg4WRllE93cLNXjMjtpC11cHJjXdb8jwJV1
UBQRkXbFzM2NrpGfG+jRcezhKFCnkDqYzkE+NxSXUpN54qw6nDBZ9GOom1eDFJSyYnjH
uSF1CeOa4qQ8Fd85mNtbYgXNp60Yz/gMXsi9FJVJkZWYctZX7ewlfMI3nbcOZzqWAUQo
bMtBMnNGrr9lThnbp6LrHcBE20J8dBnm7hgEY9BN8XYHNvmrVD6+QS9RfyouVUg3V82L
ccJrKn9jfaPhjyp581kxopyV62uiPKcJgW09DVQNVGa6Ows83E+yqbpSUh4WIX5UGvOB
TgeQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=Rqux/zW7xICXUIc9WnXIOH4l/PcDnNko21xXB1E3fws=;
b=tXqhM81BB6CHpAD5NAx+tau4jAk8eCV5lOZi1VjiCGc4QnAlVr2lRU8jsad2pE/pv9
uyh1wuJ9LpcOzVhn8ZbpZx+J0VyVxIyBO9pVvVcf8XnicVn4lZA7QIK4o41CF1GbDa7e
q30DjzEfKkn7WzZg2Lwr4R07RJjWltPyMObIddbKX9whaXXtftIv5maZLTbU2cF4JtsL
noOUJcz574dpVG81qUXwsjmUvqPciZJMrmcXRgFIx72a5HHQeNviyn4sD1cTy7WQaysC
kQn7EREAM389L0GSCRcXIFKbUyC5tl6DmX3hqTzwHCskMY/Zi7GwdUL0WXn3agBqDxZB
o0zg==
X-Gm-Message-State: AJIora/dYUQWHUCP9Toqw17P9KfQlHFZEYRR8TENRDMJSMalfjyruIuq
EI9GSFc0oJS4Q73YbjdTKCrfzYAoVVvVZVPPjmBVRnPv
X-Google-Smtp-Source: AGRyM1tX4M1pmlVvWFlEY2N4Sr1v1+UuWNoJXdI8TT2V9a7sdwnuVuEpUTNW+bnGbOteIxDL3RGMP2heMtyt45FZBqw=
X-Received: by 2002:a50:ed0b:0:b0:43c:83bd:fd5d with SMTP id
j11-20020a50ed0b000000b0043c83bdfd5dmr4033306eds.237.1658879011915; Tue, 26
Jul 2022 16:43:31 -0700 (PDT)
In-Reply-To: <kqr0eh9jaj6hps56r8ue5nls1an5dhgq1q@4ax.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAPTjJmpVMTR-gabAa=YWLTgjQLRkaDtdRTvY+qYuZ-s8qovPoQ@mail.gmail.com>
X-Mailman-Original-References: <87pmhs7xch.fsf@munus.decebal.nl>
<kqr0eh9jaj6hps56r8ue5nls1an5dhgq1q@4ax.com>
 by: Chris Angelico - Tue, 26 Jul 2022 23:43 UTC

On Wed, 27 Jul 2022 at 09:28, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
>
> On Tue, 26 Jul 2022 16:38:38 +0200, Cecil Westerhof <Cecil@decebal.nl>
> declaimed the following:
>
> >I need to get a random integer. At first I tried it with:
> > from secrets import randbelow
> > index = randbelow(len(to_try))
> >
> >This works perfectly, but it took some time. So I thought I try:
> > from random import SystemRandom
> > index = SystemRandom().randint(0, len(to_try) - 1)
> >
> >A first indication is that the second version would take about two
> >times as much time as the first. Is there a reason for this, or should
> >this not be happening?
>
> Well, off the top of my head...
>
> For one generation of "index" you are first creating an instance of
> SystemRandom(), using it to generate your random integer, and then
> disposing of the instance.
>
> If you only need ONE random integer, the time difference probably
> doesn't matter. OTOH, if you need many during the run, using
>
> sr = SystemRandom()
> #stuff in some loop that generates multiple ints
> index = sr.randint(...)
>
> Hmmm, wonder if there is a speed difference between
> .randint(0, len(to_try) - 1)
> and
> .randint(1, len(to_try)) - 1
>

Probably not significant, since the same amount of arithmetic gets
done either way. But switching to single-arg randrange(len(to_try))
will definitely help, and IMO is clearer as well (since the
implication is selecting one from a group of items).

Incidentally - if you are actually trying to select a specific item,
you may want to consider random.choice.

ChrisA

Re: random.SystemRandom().randint() inefficient

<mailman.102.1658903925.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23383&group=comp.lang.python#23383

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: barry@barrys-emacs.org (Barry)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 07:38:34 +0100
Lines: 32
Message-ID: <mailman.102.1658903925.20444.python-list@python.org>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<296D2D4B-F400-429A-8559-1D9AF9658D81@barrys-emacs.org>
Mime-Version: 1.0 (1.0)
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de h5BwueePU9YMt+QU3YmZmAZXLYI+TfjQll+EOidiJZ6Q==
Return-Path: <barry@barrys-emacs.org>
X-Original-To: Python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'engineer': 0.02; 'jul':
0.04; 'kernel': 0.05; 'random': 0.05; 'cc:addr:python-list': 0.09;
'from:addr:barry': 0.09; 'linux': 0.09; 'received:217.70': 0.09;
'received:gandi.net': 0.09; 'received:mail.gandi.net': 0.09;
'cc:no real name:2**0': 0.14; 'import': 0.15; 'url:mailman': 0.15;
'2022,': 0.16; 'barry': 0.16; 'from:addr:barrys-emacs.org': 0.16;
'message-id:@barrys-emacs.org': 0.16; 'subject:() ': 0.16;
'wrote:': 0.16; 'python': 0.16; 'cc:addr:python.org': 0.20;
'version': 0.23; 'url-ip:188.166.95.178/32': 0.25; 'url-
ip:188.166.95/24': 0.25; 'url:listinfo': 0.25; 'cc:2**0': 0.25;
'url-ip:188.166/16': 0.25; 'tried': 0.26; 'url-ip:188/8': 0.31;
'python-list': 0.32; 'but': 0.32; 'there': 0.33; 'header:In-Reply-
To:1': 0.34; 'running': 0.34; 'url-ip:13.107/16': 0.38; 'two':
0.39; 'this,': 0.39; 'should': 0.40; 'skip:r 20': 0.64; 'with:':
0.64; 'time.': 0.66; 'received:217': 0.67; 'times': 0.69; 'took':
0.69; 'linkedin:': 0.84
In-Reply-To: <87pmhs7xch.fsf@munus.decebal.nl>
X-Mailer: iPad Mail (19G71)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <296D2D4B-F400-429A-8559-1D9AF9658D81@barrys-emacs.org>
X-Mailman-Original-References: <87pmhs7xch.fsf@munus.decebal.nl>
 by: Barry - Wed, 27 Jul 2022 06:38 UTC

> On 26 Jul 2022, at 16:07, Cecil Westerhof via Python-list <python-list@python.org> wrote:
>
> I need to get a random integer. At first I tried it with:
> from secrets import randbelow
> index = randbelow(len(to_try))
>
> This works perfectly, but it took some time. So I thought I try:
> from random import SystemRandom
> index = SystemRandom().randint(0, len(to_try) - 1)
>
> A first indication is that the second version would take about two
> times as much time as the first. Is there a reason for this, or should
> this not be happening?

What is the OS that you are running on and its version?
If it’s linux what is the kernel version?
What version of python and where from?

Barry

>
> --
> Cecil Westerhof
> Senior Software Engineer
> LinkedIn: http://www.linkedin.com/in/cecilwesterhof
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Re: random.SystemRandom().randint() inefficient

<87czdr7xl0.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23384&group=comp.lang.python#23384

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 10:45:47 +0200
Organization: Decebal Computing
Lines: 31
Message-ID: <87czdr7xl0.fsf@munus.decebal.nl>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<296D2D4B-F400-429A-8559-1D9AF9658D81@barrys-emacs.org>
<mailman.102.1658903925.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader01.eternal-september.org; posting-host="1923c9f3b28f8ce722fdfc67b225a546";
logging-data="2609842"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//5g+Q5+OBzy5F4paoVZ2+V3vC0xAKokU="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:4kNa5PzVsSD4pe9jdExzgDcKdsY=
sha1:79CpS8Wsvyewy7NZbjiV2fSHW48=
 by: Cecil Westerhof - Wed, 27 Jul 2022 08:45 UTC

Barry <barry@barrys-emacs.org> writes:

>> On 26 Jul 2022, at 16:07, Cecil Westerhof via Python-list <python-list@python.org> wrote:
>>
>> I need to get a random integer. At first I tried it with:
>> from secrets import randbelow
>> index = randbelow(len(to_try))
>>
>> This works perfectly, but it took some time. So I thought I try:
>> from random import SystemRandom
>> index = SystemRandom().randint(0, len(to_try) - 1)
>>
>> A first indication is that the second version would take about two
>> times as much time as the first. Is there a reason for this, or should
>> this not be happening?
>
> What is the OS that you are running on and its version?
> If it’s linux what is the kernel version?
> What version of python and where from?

That is always good information of-course.
Debian 11.3
5.10.0-13-amd64
3.9.2

What do you mean with where the python version is from?

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: random.SystemRandom().randint() inefficient

<878roe9avv.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23385&group=comp.lang.python#23385

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 11:13:08 +0200
Organization: Decebal Computing
Lines: 158
Message-ID: <878roe9avv.fsf@munus.decebal.nl>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
<mailman.99.1658866418.20444.python-list@python.org>
<87h7338s1c.fsf@munus.decebal.nl>
<CAPTjJmpPA57iF3d6nRHgzPtdwW_VpcQSHGe_NK5qau0FJGnnGg@mail.gmail.com>
<mailman.100.1658875454.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="1923c9f3b28f8ce722fdfc67b225a546";
logging-data="2612413"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ayalwjihWRsPkuO7X+2r1ifikz4xmMVM="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:+nmqaDkxlJl8UZczg9ckzlw00u8=
sha1:H5k0SNkQEABv42VrDkPFeeWg6JI=
 by: Cecil Westerhof - Wed, 27 Jul 2022 09:13 UTC

Chris Angelico <rosuav@gmail.com> writes:

> On Wed, 27 Jul 2022 at 08:18, Cecil Westerhof via Python-list
> <python-list@python.org> wrote:
>>
>> Chris Angelico <rosuav@gmail.com> writes:
>>
>> > On Wed, 27 Jul 2022 at 06:06, Cecil Westerhof via Python-list
>> > <python-list@python.org> wrote:
>> >>
>> >> Chris Angelico <rosuav@gmail.com> writes:
>> >>
>> >> > On Wed, 27 Jul 2022 at 01:06, Cecil Westerhof via Python-list
>> >> > <python-list@python.org> wrote:
>> >> >>
>> >> >> I need to get a random integer. At first I tried it with:
>> >> >> from secrets import randbelow
>> >> >> index = randbelow(len(to_try))
>> >> >>
>> >> >> This works perfectly, but it took some time. So I thought I try:
>> >> >> from random import SystemRandom
>> >> >> index = SystemRandom().randint(0, len(to_try) - 1)
>> >> >>
>> >> >> A first indication is that the second version would take about two
>> >> >> times as much time as the first. Is there a reason for this, or should
>> >> >> this not be happening?
>> >> >>
>> >> >
>> >> > You're setting up a brand new SystemRandom instance just for a single
>> >> > random number. For a fairer comparison, set up the instance, then
>> >> > generate far more than just a single number, and see how that goes.
>> >>
>> >> Thanks. I thought I did something wrong and I did.
>> >> I will try to implement like you said and look what the result will
>> >> be. (And share it.)
>> >
>> > Thanks! Don't feel bad; performance testing is *hard*, getting
>> > meaningful results takes a lot of of fiddling with parameters, and
>> > getting interesting AND meaningful results can sometimes seem about
>> > impossible.
>> >
>> >> (As I understand it both do more, or less the same and should have
>> >> comparable performance.)
>> >
>> > In normal production work? Yes (the SystemRandom object doesn't have
>> > any significant state - a seeded RNG could have a lot more overhead
>> > here). But for performance testing? The work of instantiating the
>> > class could be completely irrelevant, or it could be dominating your
>> > results. It's hard to say, hence the suggestion to try it without
>> > reinstantiating.
>>
>> It had a very big influence. Original it took about three times more
>> time to run my program. (The program was still running when I posted
>> the original post and the difference was higher as I anticipated.)
>> Removing that did cut about 45% of the execution time of the program.
>> (So the initiation is quit expensive.)
>> But it still takes about 50% more time. So I am still a bit
>> flabbergasted.
>>
>> The new code:
>> from random import SystemRandom
>> system_random = SystemRandom()
>> index = system_random.randint(0, len(to_try) - 1)
>>
>> The first two statements are executed once.
>> The last statement I think about 75 * 10 ** 6.
>>
>> So it seems that my first idea of using randbelow was the correct one.
>> But if anyone could explain why SystemRandom is so much more
>> expensive, I would be interested to know it.
>> (Or am I still doing something wrong?)
>
> Hmm. There are still a lot of differences here. Are you able to make
> use of randrange() instead, to make them more consistent?
>
> According to the source code, secrets.randbelow is calling on an
> internal method _randbelow of the SystemRandom object, but randrange
> (if called with only one arg) will go straight into that same method.
> Here's my results:
>
> rosuav@sikorsky:~$ python3 -m timeit -s 'from random import randrange'
> 'randrange(10000)'
> 1000000 loops, best of 5: 322 nsec per loop
> rosuav@sikorsky:~$ python3 -m timeit -s 'from random import
> SystemRandom; r = SystemRandom()' 'r.randint(0, 10000)'
> 200000 loops, best of 5: 1.92 usec per loop
> rosuav@sikorsky:~$ python3 -m timeit -s 'from random import
> SystemRandom; r = SystemRandom()' 'r.randrange(10000)'
> 200000 loops, best of 5: 1.87 usec per loop
> rosuav@sikorsky:~$ python3 -m timeit -s 'from secrets import
> randbelow' 'randbelow(10000)'
> 200000 loops, best of 5: 1.64 usec per loop
>
> (The difference with the first one is that it isn't using the system
> RNG, so it has the limitations of an internal PRNG.)
>
> When you call randint, what happens is (1) the endpoint is incremented
> to transform it from inclusive-inclusive to inclusive-exclusive; (2)
> randrange is called with two args; (3) fast path 1 is skipped, fast
> path 2 is taken, and _randbelow gets called to get an actual random
> number, which gets zero added to it before returning.
>
> If, instead, you use randrange(len(to_try)), what would happen is (1)
> fast path 1 is used, and (2) _randbelow is called to get the random
> number.
>
> In secrets.randbelow, it's even better: (1) _randbelow is called to
> get the random number.
>
> I think that might be what's going on here. You're adding some tiny
> amounts of extra work, but if you were to make them more similar, the
> distinctions would evaporate. Here's a couple of other variants that
> use SystemRandom:
>
> rosuav@sikorsky:~$ python3 -m timeit -s 'from random import
> SystemRandom; randrange = SystemRandom().randrange' 'randrange(10000)'
> 200000 loops, best of 5: 1.81 usec per loop
> rosuav@sikorsky:~$ python3 -m timeit -s 'from random import
> SystemRandom; randrange = SystemRandom()._randbelow'
> 'randrange(10000)'
> 200000 loops, best of 5: 1.61 usec per loop
>
> Note that we shave a few usec by avoiding the attribute lookup, but
> even more by directly calling _randbelow, which is why
> secrets.randbelow is able to save a bit. But don't use internal
> methods; you can probably get pretty much equivalent performance from
> randrange, which is public.

The times I use are user and sys added. Real can widely differ, but
user and sys added should be reasonable constant. (But is not always.)
The SystemRandom versions without initiation again and again.
This are my results until now for running the complete program:
randbelow: 13 min
randint: 21 min
randrange: 15 min
randrange2: 14 min

randrange is:
system_random = SystemRandom()
index = system_random.randrange(len(to_try))

randrange2 is:
randrange = SystemRandom().randrange
index = randrange(len(to_try))

The strange thing is that the first time I executed randrange2 the
time jumped to 20 minutes. But maybe that was because the 'entropy'
was used up?

I think I stick with the randbelow version.
Or is there a reason that it is, or could be better to stay with the
randrange version? It was introduced in python 3.6. Should I take into
account that people can work with an older version?

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: random.SystemRandom().randint() inefficient

<87zggu7vmo.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23386&group=comp.lang.python#23386

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 11:27:59 +0200
Organization: Decebal Computing
Lines: 15
Message-ID: <87zggu7vmo.fsf@munus.decebal.nl>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<kqr0eh9jaj6hps56r8ue5nls1an5dhgq1q@4ax.com>
<CAPTjJmpVMTR-gabAa=YWLTgjQLRkaDtdRTvY+qYuZ-s8qovPoQ@mail.gmail.com>
<mailman.101.1658879014.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="1923c9f3b28f8ce722fdfc67b225a546";
logging-data="2617840"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KIVD2Uibmc6+htFcVpXNvXixKDLicNyU="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:y+oge7+jr4o33hgiBHiy3l6u5YI=
sha1:Z/P087npJdYfGNn+wgICNOQ+Uao=
 by: Cecil Westerhof - Wed, 27 Jul 2022 09:27 UTC

Chris Angelico <rosuav@gmail.com> writes:

> Incidentally - if you are actually trying to select a specific item,
> you may want to consider random.choice.

Yes, I try to select a random element, but it has also to be removed.
An element should be used at most once. This is the code I use:
# index = randbelow(len(to_try))
index = randrange(len(to_try))
found = permutation[to_try.pop(index)]

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: random.SystemRandom().randint() inefficient

<874jz29apa.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23387&group=comp.lang.python#23387

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 11:17:05 +0200
Organization: Decebal Computing
Lines: 36
Message-ID: <874jz29apa.fsf@munus.decebal.nl>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
<mailman.99.1658866418.20444.python-list@python.org>
<87h7338s1c.fsf@munus.decebal.nl>
<dgs0ehps5h73vob431svjnt3d7pqmv8cds@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="1923c9f3b28f8ce722fdfc67b225a546";
logging-data="2617840"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18a8VoUqz45jZNWoN2xD7naRoS64N3+1Vo="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:uNPnajSW3FyaWC191EQqKd8xynI=
sha1:lvn8vk44XZMbOmdwXIbJydpVkcg=
 by: Cecil Westerhof - Wed, 27 Jul 2022 09:17 UTC

Dennis Lee Bieber <wlfraed@ix.netcom.com> writes:

> On Tue, 26 Jul 2022 23:47:59 +0200, Cecil Westerhof <Cecil@decebal.nl>
> declaimed the following:
>
>
>>The new code:
>> from random import SystemRandom
>> system_random = SystemRandom()
>> index = system_random.randint(0, len(to_try) - 1)
>>
>>The first two statements are executed once.
>>The last statement I think about 75 * 10 ** 6.
>>
>>So it seems that my first idea of using randbelow was the correct one.
>>But if anyone could explain why SystemRandom is so much more
>>expensive, I would be interested to know it.
>>(Or am I still doing something wrong?)
>
> What happens with
>
> system_randint = SystemRandom().randint #no parens
>
> index = system_randint(...)
>
> which may remove the method lookup from the repetition.

I had already switched to randrange. This went to 15 minutes from 21
minutes.
By removing the method lookup I could shave off another minute. So
certainly noteworthy. (Should have thought about it myself.)

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: random.SystemRandom().randint() inefficient

<tbre1g$2h6a1$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23389&group=comp.lang.python#23389

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: michael.stemper@gmail.com (Michael F. Stemper)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 08:19:35 -0500
Organization: A noiseless patient Spider
Lines: 84
Message-ID: <tbre1g$2h6a1$1@dont-email.me>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
<mailman.99.1658866418.20444.python-list@python.org>
<87h7338s1c.fsf@munus.decebal.nl>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 27 Jul 2022 13:19:44 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c4155ac7c2702bc82a6d10924c7da7ee";
logging-data="2660673"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1913FdXwtO2C50MvsNvs7lp1vJ4FwWA+bc="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101
Thunderbird/68.10.0
Cancel-Lock: sha1:EFoHfFXUGV650aw84YRM9Yc1i+s=
Content-Language: en-US
In-Reply-To: <87h7338s1c.fsf@munus.decebal.nl>
 by: Michael F. Stemper - Wed, 27 Jul 2022 13:19 UTC

On 26/07/2022 16.47, Cecil Westerhof wrote:
> Chris Angelico <rosuav@gmail.com> writes:
>
>> On Wed, 27 Jul 2022 at 06:06, Cecil Westerhof via Python-list
>> <python-list@python.org> wrote:
>>>
>>> Chris Angelico <rosuav@gmail.com> writes:
>>>
>>>> On Wed, 27 Jul 2022 at 01:06, Cecil Westerhof via Python-list
>>>> <python-list@python.org> wrote:
>>>>>
>>>>> I need to get a random integer. At first I tried it with:
>>>>> from secrets import randbelow
>>>>> index = randbelow(len(to_try))
>>>>>
>>>>> This works perfectly, but it took some time. So I thought I try:
>>>>> from random import SystemRandom
>>>>> index = SystemRandom().randint(0, len(to_try) - 1)
>>>>>
>>>>> A first indication is that the second version would take about two
>>>>> times as much time as the first. Is there a reason for this, or should
>>>>> this not be happening?
>>>>>
>>>>
>>>> You're setting up a brand new SystemRandom instance just for a single
>>>> random number. For a fairer comparison, set up the instance, then
>>>> generate far more than just a single number, and see how that goes.
>>>
>>> Thanks. I thought I did something wrong and I did.
>>> I will try to implement like you said and look what the result will
>>> be. (And share it.)
>>
>> Thanks! Don't feel bad; performance testing is *hard*, getting
>> meaningful results takes a lot of of fiddling with parameters, and
>> getting interesting AND meaningful results can sometimes seem about
>> impossible.
>>
>>> (As I understand it both do more, or less the same and should have
>>> comparable performance.)
>>
>> In normal production work? Yes (the SystemRandom object doesn't have
>> any significant state - a seeded RNG could have a lot more overhead
>> here). But for performance testing? The work of instantiating the
>> class could be completely irrelevant, or it could be dominating your
>> results. It's hard to say, hence the suggestion to try it without
>> reinstantiating.
>
> It had a very big influence. Original it took about three times more
> time to run my program. (The program was still running when I posted
> the original post and the difference was higher as I anticipated.)
> Removing that did cut about 45% of the execution time of the program.
> (So the initiation is quit expensive.)
> But it still takes about 50% more time. So I am still a bit
> flabbergasted.
>
> The new code:
> from random import SystemRandom
> system_random = SystemRandom()
> index = system_random.randint(0, len(to_try) - 1)

This is orthogonal to your question, but might be of some use to you:

The combination of using len(to_try) as an argument to randint() and
saving the output to a variable named "index" suggests that you might
be setting up to select a random element from to_try, as in:
something = to_try[index]

If that is the case, you might want to consider using random.choice() instead:

>>> from random import choice
>>> to_try = [2,3,5,7,11,13,"seventeen",19]
>>> choice(to_try)
2
>>> choice(to_try)
'seventeen'
>>> choice(to_try)
13
>>> choice(to_try)
5
>>>

--
Michael F. Stemper
This sentence no verb.

Re: random.SystemRandom().randint() inefficient

<87mtcu7e8w.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23391&group=comp.lang.python#23391

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 17:43:27 +0200
Organization: Decebal Computing
Lines: 34
Message-ID: <87mtcu7e8w.fsf@munus.decebal.nl>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
<mailman.99.1658866418.20444.python-list@python.org>
<87h7338s1c.fsf@munus.decebal.nl> <tbre1g$2h6a1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="1923c9f3b28f8ce722fdfc67b225a546";
logging-data="2693918"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18iQqirfCj+R5yufFvNk00gOWkkHCC5+6U="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:+JHwZkyii2Od1Jf9WKy11B5ezn8=
sha1:C5JJOHu9L9APNYBHzLqy/GFVwVU=
 by: Cecil Westerhof - Wed, 27 Jul 2022 15:43 UTC

"Michael F. Stemper" <michael.stemper@gmail.com> writes:

> This is orthogonal to your question, but might be of some use to you:
>
> The combination of using len(to_try) as an argument to randint() and
> saving the output to a variable named "index" suggests that you might
> be setting up to select a random element from to_try, as in:
> something = to_try[index]
>
> If that is the case, you might want to consider using random.choice() instead:
>
> >>> from random import choice
> >>> to_try = [2,3,5,7,11,13,"seventeen",19]
> >>> choice(to_try)
> 2
> >>> choice(to_try)
> 'seventeen'
> >>> choice(to_try)
> 13
> >>> choice(to_try)
> 5
> >>>

Yes, I try to select a random element, but it has also to be removed,
because an element should not be used more as once.
This is the code I use:
# index = randbelow(len(to_try))
index = randrange(len(to_try))
found = permutation[to_try.pop(index)]

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: random.SystemRandom().randint() inefficient

<jto2ehtvhl82ndmhtk1fpeb0amo3ig1dku@4ax.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23394&group=comp.lang.python#23394

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 27 Jul 2022 11:13:37 -0500
From: wlfraed@ix.netcom.com (Dennis Lee Bieber)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 12:13:38 -0400
Organization: IISS Elusive Unicorn
Message-ID: <jto2ehtvhl82ndmhtk1fpeb0amo3ig1dku@4ax.com>
References: <87pmhs7xch.fsf@munus.decebal.nl> <296D2D4B-F400-429A-8559-1D9AF9658D81@barrys-emacs.org> <mailman.102.1658903925.20444.python-list@python.org> <87czdr7xl0.fsf@munus.decebal.nl>
User-Agent: ForteAgent/8.00.32.1272
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 15
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-hupu8OEv7RfbPzsRJpzEMBjB2zm8GQA5Gykh+XoQSA7Ja5yKvdAjQcwyCcfZHCh6RBSV5yk34l4gngb!iM0cIuCN1HSp+E6RyuxrFYcsCxou/EWWz7hN5zfBL4vrygsVFvbkM/yxlba/ZQVUDX9ctWKd
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 1744
 by: Dennis Lee Bieber - Wed, 27 Jul 2022 16:13 UTC

On Wed, 27 Jul 2022 10:45:47 +0200, Cecil Westerhof <Cecil@decebal.nl>
declaimed the following:

>What do you mean with where the python version is from?

Base Python.org download, ActiveState package download, Anaconda
package download, native OS install/extra install via OS repository
download (Debian/Ubuntu: apt install xxx, where xxx is not the native OS
Python)

--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

Re: random.SystemRandom().randint() inefficient

<mailman.105.1658939637.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23396&group=comp.lang.python#23396

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: python@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 17:30:48 +0100
Lines: 37
Message-ID: <mailman.105.1658939637.20444.python-list@python.org>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
<mailman.99.1658866418.20444.python-list@python.org>
<87h7338s1c.fsf@munus.decebal.nl> <tbre1g$2h6a1$1@dont-email.me>
<87mtcu7e8w.fsf@munus.decebal.nl>
<3f359767-dffc-2984-afd3-efb8323ae3c2@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de XiGMJe7Z8zatAC/1onurWANv657wBEpWzX3Q/VXtgmCQ==
Return-Path: <python@mrabarnett.plus.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=plus.com header.i=@plus.com header.b=h03T33x4;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.006
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'argument': 0.04; 'random':
0.05; 'variable': 0.05; 'from:addr:python': 0.09; 'moved': 0.09;
'received:192.168.1.64': 0.09; 'skip:[ 30': 0.09; 'writes:': 0.09;
'import': 0.15; 'from:addr:mrabarnett.plus.com': 0.16;
'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16;
'once.': 0.16; 'question,': 0.16; 'received:plus.net': 0.16;
'shuffling': 0.16; 'subject:() ': 0.16; 'use:': 0.16; 'wrote:':
0.16; 'to:addr:python-list': 0.20; 'code': 0.23; 'skip:p 30':
0.23; '>>>': 0.28; 'output': 0.28; 'header:User-Agent:1': 0.30;
'takes': 0.31; 'elements': 0.32; 'end.': 0.32; 'python-list':
0.32; 'received:192.168.1': 0.32; 'but': 0.32; 'header:In-Reply-
To:1': 0.34; 'yes,': 0.35; 'using': 0.37; 'received:192.168':
0.37; 'setting': 0.39; 'list': 0.39; 'use': 0.39; 'something':
0.40; 'want': 0.40; 'try': 0.40; 'should': 0.40; 'received:212':
0.62; 'skip:r 20': 0.64; 'in:': 0.64; 'your': 0.64; 'named': 0.65;
'time.': 0.66; 'choice': 0.76; 'combination': 0.76; 'you:': 0.84;
'down,': 0.91; 'pop': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1658939450; bh=Y+fJVa420nB9dKLI/quIXPOnGlp5wh4noA/WmgzpnuY=;
h=Date:Subject:To:References:From:In-Reply-To;
b=h03T33x4+1W49CihORPdncIeEGj3MT4eCsO/e6jCBcHBJ6WRRj8P5VctMZk44L9ms
9SImVNsfHX0uC/rSVwAjd0hzZgmIDCVp9f0GPjdnP+MSyyfpq59nw4tR6ggByBGSL1
kuj8jbNKV7+YbuWeHSKXBS23fmRF3/hBGjpT38XRyiakDwWVSz4GzOaJlQQjzotVB+
E57fJWTiocqUvJgWVhXu7Pt1vABUVbLIs+JXU9XDfFttsMD4sH16J7Cc+pNOSLhLxF
pEtiCk/SLngAlWE2tH1QYwr5E035JhIj7qVqCyj3WwPZE5E4rD6eNIeJSS6jLdepuQ
FCQpwjWGchhZg==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=T+Gf8tGQ c=1 sm=1 tr=0 ts=62e1683a
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=IkcTkHD0fZMA:10 a=pGLkceISAAAA:8 a=oT6JsUI75pGswSwWvjoA:9 a=QEXdDO2ut3YA:10
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Content-Language: en-GB
In-Reply-To: <87mtcu7e8w.fsf@munus.decebal.nl>
X-CMAE-Envelope: MS4xfEPhWx7xZqaoRJnme32tkSIuKx19/iJE4Uf2SLGjxwx5rB1wMZzUTTa2KydK0YfaOKkZ+LSxpMAKnhTsXtYtnGqXHXecMJyjftpnbbjbqmPzl8jHPTvz
XNfUcegZ/U5wBmmEjMgTVvg2M2jZ4PDeAcrl3q6ywfRR5mT/ewQenZir8YoYgOwESEmkshRLLAF8HZ0eL+nI4awO7BvtT9CowW8=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <3f359767-dffc-2984-afd3-efb8323ae3c2@mrabarnett.plus.com>
X-Mailman-Original-References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
<mailman.99.1658866418.20444.python-list@python.org>
<87h7338s1c.fsf@munus.decebal.nl> <tbre1g$2h6a1$1@dont-email.me>
<87mtcu7e8w.fsf@munus.decebal.nl>
 by: MRAB - Wed, 27 Jul 2022 16:30 UTC

On 27/07/2022 16:43, Cecil Westerhof via Python-list wrote:
> "Michael F. Stemper" <michael.stemper@gmail.com> writes:
>
>> This is orthogonal to your question, but might be of some use to you:
>>
>> The combination of using len(to_try) as an argument to randint() and
>> saving the output to a variable named "index" suggests that you might
>> be setting up to select a random element from to_try, as in:
>> something = to_try[index]
>>
>> If that is the case, you might want to consider using random.choice() instead:
>>
>> >>> from random import choice
>> >>> to_try = [2,3,5,7,11,13,"seventeen",19]
>> >>> choice(to_try)
>> 2
>> >>> choice(to_try)
>> 'seventeen'
>> >>> choice(to_try)
>> 13
>> >>> choice(to_try)
>> 5
>> >>>
>
> Yes, I try to select a random element, but it has also to be removed,
> because an element should not be used more as once.
> This is the code I use:
> # index = randbelow(len(to_try))
> index = randrange(len(to_try))
> found = permutation[to_try.pop(index)]
>

When you pop an element from the last, the elements after it need to be
moved down, which takes time.

Try shuffling the list and then popping the now randomly-ordered
elements off the end.

Re: random.SystemRandom().randint() inefficient

<87edy679ou.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23398&group=comp.lang.python#23398

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 19:21:53 +0200
Organization: Decebal Computing
Lines: 19
Message-ID: <87edy679ou.fsf@munus.decebal.nl>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<296D2D4B-F400-429A-8559-1D9AF9658D81@barrys-emacs.org>
<mailman.102.1658903925.20444.python-list@python.org>
<87czdr7xl0.fsf@munus.decebal.nl>
<jto2ehtvhl82ndmhtk1fpeb0amo3ig1dku@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="1923c9f3b28f8ce722fdfc67b225a546";
logging-data="2716790"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Rb4Nu0a5sVl7t/Vz00cwHtYIt2SlcTkY="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:LemKMuGdqefm6x3YkN2QXQaBahg=
sha1:vggpjwA0eQPVXR1sIO4mqnY4Ieo=
 by: Cecil Westerhof - Wed, 27 Jul 2022 17:21 UTC

Dennis Lee Bieber <wlfraed@ix.netcom.com> writes:

> On Wed, 27 Jul 2022 10:45:47 +0200, Cecil Westerhof <Cecil@decebal.nl>
> declaimed the following:
>
>
>>What do you mean with where the python version is from?
>
> Base Python.org download, ActiveState package download, Anaconda
> package download, native OS install/extra install via OS repository
> download (Debian/Ubuntu: apt install xxx, where xxx is not the native OS
> Python)

Just the default Debian install.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: random.SystemRandom().randint() inefficient

<87a68u79kx.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23399&group=comp.lang.python#23399

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 19:24:14 +0200
Organization: Decebal Computing
Lines: 46
Message-ID: <87a68u79kx.fsf@munus.decebal.nl>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
<mailman.99.1658866418.20444.python-list@python.org>
<87h7338s1c.fsf@munus.decebal.nl> <tbre1g$2h6a1$1@dont-email.me>
<87mtcu7e8w.fsf@munus.decebal.nl>
<3f359767-dffc-2984-afd3-efb8323ae3c2@mrabarnett.plus.com>
<mailman.105.1658939637.20444.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="1923c9f3b28f8ce722fdfc67b225a546";
logging-data="2716790"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19BHjqH82p3Xw1hNrSzpp+O/gw0KlfDqTI="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:ACZjAzR4LtkMcP2QPuaQtT6/q7g=
sha1:H2mTAcd51rJbe/RYyvTvrO2jOr4=
 by: Cecil Westerhof - Wed, 27 Jul 2022 17:24 UTC

MRAB <python@mrabarnett.plus.com> writes:

> On 27/07/2022 16:43, Cecil Westerhof via Python-list wrote:
>> "Michael F. Stemper" <michael.stemper@gmail.com> writes:
>>
>>> This is orthogonal to your question, but might be of some use to you:
>>>
>>> The combination of using len(to_try) as an argument to randint() and
>>> saving the output to a variable named "index" suggests that you might
>>> be setting up to select a random element from to_try, as in:
>>> something = to_try[index]
>>>
>>> If that is the case, you might want to consider using random.choice() instead:
>>>
>>> >>> from random import choice
>>> >>> to_try = [2,3,5,7,11,13,"seventeen",19]
>>> >>> choice(to_try)
>>> 2
>>> >>> choice(to_try)
>>> 'seventeen'
>>> >>> choice(to_try)
>>> 13
>>> >>> choice(to_try)
>>> 5
>>> >>>
>> Yes, I try to select a random element, but it has also to be removed,
>> because an element should not be used more as once.
>> This is the code I use:
>> # index = randbelow(len(to_try))
>> index = randrange(len(to_try))
>> found = permutation[to_try.pop(index)]
>>
>
> When you pop an element from the last, the elements after it need to be
> moved down, which takes time.
>
> Try shuffling the list and then popping the now randomly-ordered
> elements off the end.

Would shuffling not be a lot more expensive? Especially because I do
not eat the whole list.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: random.SystemRandom().randint() inefficient

<mailman.106.1658943480.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23400&group=comp.lang.python#23400

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: mats@wichmann.us (Mats Wichmann)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 11:37:44 -0600
Lines: 17
Message-ID: <mailman.106.1658943480.20444.python-list@python.org>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<296D2D4B-F400-429A-8559-1D9AF9658D81@barrys-emacs.org>
<mailman.102.1658903925.20444.python-list@python.org>
<87czdr7xl0.fsf@munus.decebal.nl>
<6a4912ad-bd5a-f159-f413-ab3fa37eeb6e@wichmann.us>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de su0Ts4LaeFJ+Vc6Xq5LBKAHqs5YZ8jwE3n2qxIqsMVeg==
Return-Path: <mats@wichmann.us>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="1024-bit key; unprotected key"
header.d=pobox.com header.i=@pobox.com header.b=L8LQYrl7;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.021
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'windows,': 0.05; 'debian':
0.09; 'writes:': 0.09; 'received:173': 0.13; 'barry': 0.16;
'package".': 0.16; 'proportion': 0.16; 'subject:() ': 0.16;
'tends': 0.16; 'wrote:': 0.16; 'python': 0.16; 'to:addr:python-
list': 0.20; 'version': 0.23; 'asked': 0.29; 'header:User-
Agent:1': 0.30; 'question': 0.32; 'here,': 0.32; 'python-list':
0.32; 'there': 0.33; 'mean': 0.34; 'header:In-Reply-To:1': 0.34;
'people': 0.36; 'received:192.168': 0.37; 'here.': 0.61; 'thus':
0.64; 'your': 0.64; 'store,': 0.69; 'matters,': 0.84; 'implied':
0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=message-id
:date:mime-version:subject:to:references:from:in-reply-to
:content-type:content-transfer-encoding; s=sasl; bh=xGfYzCFG+/vD
kZDmFN8ENPn/J18K4rqYoGXtddtlU6U=; b=L8LQYrl7cO52J9dG3/g6M3eLn7zv
ATFcgNiZdrp4LfeUTXltcccgPKyWyFS8oT1NXOUWII5TzP1UCAlpTg8kFsDAfPDP
r2RwOh4vRUf5ytN62NZzQGR/Be9sil7RQWUMOtlM8j145wrRuszpQDLAdKXNhVhc
DQ/jMuV0U6O4hHM=
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=wichmann.us;
h=message-id:date:mime-version:subject:to:references:from:in-reply-to:content-type:content-transfer-encoding;
s=2018-07.pbsmtp; bh=xGfYzCFG+/vDkZDmFN8ENPn/J18K4rqYoGXtddtlU6U=;
b=DMPhN5+uFUbf8aUCumsoXNccA4IHZcouUiL22b3uwU6dxu8CfSkBWZIvktBm9se30ofgOfUNWEsBDTe/7cACN0nIpIRhiBhkGlIJOHzNAy1wQoHHg48ZAHKyS0fvm242j4JtG4Dm008rB6J0He0/M57BLI2apgho9akicrc0ZN4=
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.11.0
Content-Language: en-US
In-Reply-To: <87czdr7xl0.fsf@munus.decebal.nl>
X-Pobox-Relay-ID: D37CE10E-0DD2-11ED-AD29-C85A9F429DF0-81526775!pb-smtp20.pobox.com
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <6a4912ad-bd5a-f159-f413-ab3fa37eeb6e@wichmann.us>
X-Mailman-Original-References: <87pmhs7xch.fsf@munus.decebal.nl>
<296D2D4B-F400-429A-8559-1D9AF9658D81@barrys-emacs.org>
<mailman.102.1658903925.20444.python-list@python.org>
<87czdr7xl0.fsf@munus.decebal.nl>
 by: Mats Wichmann - Wed, 27 Jul 2022 17:37 UTC

On 7/27/22 02:45, Cecil Westerhof via Python-list wrote:
> Barry <barry@barrys-emacs.org> writes:

>> What version of python and where from?
>
> That is always good information of-course.
> Debian 11.3
> 5.10.0-13-amd64
> 3.9.2
>
> What do you mean with where the python version is from?

On Windows, the platform of a large proportion of people asking
questions here, there are many possible Python builds (python.org,
Microsoft Store, Conda, ActiveState, etc.) and it sometimes matters,
thus that tends to be a standard question that gets asked here. In your
case the implied answer is "distro package".

Re: random.SystemRandom().randint() inefficient

<mailman.108.1658944850.20444.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23403&group=comp.lang.python#23403

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: barry@barrys-emacs.org (Barry)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 19:00:42 +0100
Lines: 46
Message-ID: <mailman.108.1658944850.20444.python-list@python.org>
References: <87czdr7xl0.fsf@munus.decebal.nl>
<473B634B-494D-47BC-A2AB-1F4DD707DF03@barrys-emacs.org>
Mime-Version: 1.0 (1.0)
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de ZrUFiQgmqS4Hn1dX6jR2SQ61QXXhjaII+Kj10Zj1tWxg==
Return-Path: <barry@barrys-emacs.org>
X-Original-To: Python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'engineer': 0.02; 'jul':
0.04; 'kernel': 0.05; 'random': 0.05; 'cc:addr:python-list': 0.09;
'debian': 0.09; 'from:addr:barry': 0.09; 'linux': 0.09;
'received:217.70': 0.09; 'received:gandi.net': 0.09;
'received:mail.gandi.net': 0.09; 'writes:': 0.09; 'cc:no real
name:2**0': 0.14; 'import': 0.15; 'url:mailman': 0.15; '2022,':
0.16; '>>>>': 0.16; 'barry': 0.16; 'comparing': 0.16; 'from:addr
:barrys-emacs.org': 0.16; 'message-id:@barrys-emacs.org': 0.16;
'python\xe2\x80\x99s': 0.16; 'subject:() ': 0.16; 'wrote:': 0.16;
'python': 0.16; 'cc:addr:python.org': 0.20; 'version': 0.23; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'depends': 0.25; 'url:listinfo': 0.25; 'cc:2**0': 0.25; 'url-
ip:188.166/16': 0.25; 'tried': 0.26; '>>>': 0.28; 'url-ip:188/8':
0.31; 'python-list': 0.32; 'but': 0.32; 'there': 0.33; 'mean':
0.34; 'header:In-Reply-To:1': 0.34; 'running': 0.34; 'url-
ip:13.107/16': 0.38; 'two': 0.39; 'this,': 0.39; 'should': 0.40;
'skip:r 20': 0.64; 'with:': 0.64; 'time.': 0.66; 'received:217':
0.67; 'generator': 0.69; 'times': 0.69; 'took': 0.69; 'linkedin:':
0.84
In-Reply-To: <87czdr7xl0.fsf@munus.decebal.nl>
X-Mailer: iPad Mail (19G71)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <473B634B-494D-47BC-A2AB-1F4DD707DF03@barrys-emacs.org>
X-Mailman-Original-References: <87czdr7xl0.fsf@munus.decebal.nl>
 by: Barry - Wed, 27 Jul 2022 18:00 UTC

> On 27 Jul 2022, at 17:09, Cecil Westerhof via Python-list <python-list@python.org> wrote:
>
> Barry <barry@barrys-emacs.org> writes:
>
>>>> On 26 Jul 2022, at 16:07, Cecil Westerhof via Python-list <python-list@python.org> wrote:
>>>
>>> I need to get a random integer. At first I tried it with:
>>> from secrets import randbelow
>>> index = randbelow(len(to_try))
>>>
>>> This works perfectly, but it took some time. So I thought I try:
>>> from random import SystemRandom
>>> index = SystemRandom().randint(0, len(to_try) - 1)
>>>
>>> A first indication is that the second version would take about two
>>> times as much time as the first. Is there a reason for this, or should
>>> this not be happening?
>>
>> What is the OS that you are running on and its version?
>> If it’s linux what is the kernel version?
>> What version of python and where from?
>
> That is always good information of-course.
> Debian 11.3
> 5.10.0-13-amd64
> 3.9.2
>
> What do you mean with where the python version is from?

Because random number generator implementation depends on the
OS since it is SystemRandom() that you are comparing with python’s
Implementation.

Barry
>
> --
> Cecil Westerhof
> Senior Software Engineer
> LinkedIn: http://www.linkedin.com/in/cecilwesterhof
> --
> https://mail.python.org/mailman/listinfo/python-list

Re: random.SystemRandom().randint() inefficient

<86a68utn0w.fsf@williamsburg.bawden.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23405&group=comp.lang.python#23405

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: alan@csail.mit.edu (Alan Bawden)
Newsgroups: comp.lang.python
Subject: Re: random.SystemRandom().randint() inefficient
Date: Wed, 27 Jul 2022 14:42:55 -0400
Organization: ITS Preservation Society
Lines: 27
Message-ID: <86a68utn0w.fsf@williamsburg.bawden.org>
References: <87pmhs7xch.fsf@munus.decebal.nl>
<CAPTjJmoVn1pFjw-m_xtEgFdz+EiZs_H=+9Z9jZEEbQHTRvkR6g@mail.gmail.com>
<mailman.96.1658858493.20444.python-list@python.org>
<87lesf8ylo.fsf@munus.decebal.nl>
<CAPTjJmrETswOqZ8DJF2eK9ENAyoc57MfiegXshHCDoLRYKpi9w@mail.gmail.com>
<mailman.99.1658866418.20444.python-list@python.org>
<87h7338s1c.fsf@munus.decebal.nl> <tbre1g$2h6a1$1@dont-email.me>
<87mtcu7e8w.fsf@munus.decebal.nl>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="8d18119746807bf5b895669693522346";
logging-data="2733737"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/7AjlZplfmivUfme47hr6l"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock: sha1:c1LO6q6u6c+/xdONpFfpb5VlFbw=
sha1:HTcsZG0Z37oSZ0D0Hb4REzkCP3w=
 by: Alan Bawden - Wed, 27 Jul 2022 18:42 UTC

Cecil Westerhof <Cecil@decebal.nl> writes:

Yes, I try to select a random element, but it has also to be removed,
because an element should not be used more as once.

Instead of using pop to do that why not something like:

def lazy_shuffle(seq):
"""
Generate the elements of the given sequence in a random order.
"""
# Delete the next line if you want to use a different version of
# randrange:
from random import randrange
# Delete the next line if SEQ is already a mutable sequence and you
# are willing to have it destroyed by this process:
seq = list(seq)
n = len(seq)
while n:
i = randrange(n)
yield seq[i]
n -= 1
if i < n:
seq[i] = seq[n]

--
Alan Bawden


devel / comp.lang.python / random.SystemRandom().randint() inefficient

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor