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

2022-07-27 Thread Barry
> On 26 Jul 2022, at 16:07, Cecil Westerhof via Python-list > 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

More efficient code, but slower program

2022-07-27 Thread Cecil Westerhof via Python-list
It is not very important, but I am just curious. Original I had in a program: values = [*range(100)] But because it is done quite often I expected that initialising: range_list = [*range(100)] and then use: values = range_list.copy() Would be more efficient. So I tried:

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

2022-07-27 Thread Barry
> On 27 Jul 2022, at 17:09, Cecil Westerhof via Python-list > wrote: > > Barry writes: > On 26 Jul 2022, at 16:07, Cecil Westerhof via Python-list wrote: >>> >>> I need to get a random integer. At first I tried it with: >>> from secrets import randbelow >>> index =

Re: More efficient code, but slower program

2022-07-27 Thread Cecil Westerhof via Python-list
2qdxy4rzwzuui...@potatochowder.com writes: > On 2022-07-27 at 17:48:47 +0200, > Regarding "Re: More efficient code, but slower program," > Cecil Westerhof via Python-list wrote: > >> r...@zedat.fu-berlin.de (Stefan Ram) writes: >> >> > Cecil Westerhof writes: >> >>values = [*range(100)] >> >

Re: More efficient code, but slower program

2022-07-27 Thread 2QdxY4RzWzUUiLuE
On 2022-07-27 at 17:48:47 +0200, Regarding "Re: More efficient code, but slower program," Cecil Westerhof via Python-list wrote: > r...@zedat.fu-berlin.de (Stefan Ram) writes: > > > Cecil Westerhof writes: > >>values = [*range(100)] > > > > In many cases, any iterable is just fine and a list

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

2022-07-27 Thread Dennis Lee Bieber
On Wed, 27 Jul 2022 10:45:47 +0200, Cecil Westerhof 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

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

2022-07-27 Thread Cecil Westerhof via Python-list
MRAB writes: > On 27/07/2022 16:43, Cecil Westerhof via Python-list wrote: >> "Michael F. Stemper" 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

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

2022-07-27 Thread Cecil Westerhof via Python-list
Roel Schroeven writes: > Cecil Westerhof via Python-list schreef op 27/07/2022 om 17:43: >> "Michael F. Stemper" 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

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

2022-07-27 Thread Cecil Westerhof via Python-list
Chris Angelico 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 =

Simple TCP proxy

2022-07-27 Thread Morten W. Petersen
Hi. I'd like to share with you a recent project, which is a simple TCP proxy that can stand in front of a TCP server of some sort, queueing requests and then allowing n number of connections to pass through at a time: https://github.com/morphex/stp I'll be developing it further, but the the

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

2022-07-27 Thread Michael F. Stemper
On 26/07/2022 16.47, Cecil Westerhof wrote: Chris Angelico writes: On Wed, 27 Jul 2022 at 06:06, Cecil Westerhof via Python-list wrote: Chris Angelico writes: On Wed, 27 Jul 2022 at 01:06, Cecil Westerhof via Python-list wrote: I need to get a random integer. At first I tried it

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

2022-07-27 Thread MRAB
On 27/07/2022 16:43, Cecil Westerhof via Python-list wrote: "Michael F. Stemper" 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

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

2022-07-27 Thread Mats Wichmann
On 7/27/22 02:45, Cecil Westerhof via Python-list wrote: > Barry 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

Re: Simple TCP proxy

2022-07-27 Thread Chris Angelico
On Thu, 28 Jul 2022 at 02:15, Morten W. Petersen wrote: > > Hi. > > I'd like to share with you a recent project, which is a simple TCP proxy > that can stand in front of a TCP server of some sort, queueing requests and > then allowing n number of connections to pass through at a time: How's this

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

2022-07-27 Thread Cecil Westerhof via Python-list
Alan Bawden writes: > Cecil Westerhof 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): > """ >

Re: More efficient code, but slower program

2022-07-27 Thread Cecil Westerhof via Python-list
r...@zedat.fu-berlin.de (Stefan Ram) writes: > Cecil Westerhof writes: >>values = [*range(100)] > > In many cases, any iterable is just fine and a list is not > required, just as peudo-random numbers often are just fine and > real-world entropy is not required. In this case both are. I

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

2022-07-27 Thread Cecil Westerhof via Python-list
Chris Angelico writes: > On Wed, 27 Jul 2022 at 08:18, Cecil Westerhof via Python-list > wrote: >> >> Chris Angelico writes: >> >> > On Wed, 27 Jul 2022 at 06:06, Cecil Westerhof via Python-list >> > wrote: >> >> >> >> Chris Angelico writes: >> >> >> >> > On Wed, 27 Jul 2022 at 01:06, Cecil

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

2022-07-27 Thread Cecil Westerhof via Python-list
Dennis Lee Bieber writes: > On Tue, 26 Jul 2022 23:47:59 +0200, Cecil Westerhof > 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

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

2022-07-27 Thread Cecil Westerhof via Python-list
Dennis Lee Bieber writes: > On Wed, 27 Jul 2022 10:45:47 +0200, Cecil Westerhof > 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

Re: More efficient code, but slower program

2022-07-27 Thread Dennis Lee Bieber
On Wed, 27 Jul 2022 14:58:02 +0200, Cecil Westerhof declaimed the following: >It is not very important, but I am just curious. > >Original I had in a program: >values = [*range(100)] > >But because it is done quite often I expected that initialising: >range_list = [*range(100)] >

Re: Simple TCP proxy

2022-07-27 Thread Morten W. Petersen
Hi Chris. You're thinking of the backlog argument of listen? Well, STP will accept all connections, but can limit how many of the accepted connections that are active at any given time. So when I bombed it with hundreds of almost simultaneous connections, all of them were accepted, but only 25

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

2022-07-27 Thread Roel Schroeven
Cecil Westerhof via Python-list schreef op 27/07/2022 om 17:43: "Michael F. Stemper" 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

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

2022-07-27 Thread Alan Bawden
Cecil Westerhof 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

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

2022-07-27 Thread MRAB
On 27/07/2022 18:24, Cecil Westerhof via Python-list wrote: MRAB writes: On 27/07/2022 16:43, Cecil Westerhof via Python-list wrote: "Michael F. Stemper" 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

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

2022-07-27 Thread Cecil Westerhof via Python-list
MRAB writes: >>> 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 >>

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

2022-07-27 Thread Cecil Westerhof via Python-list
Barry writes: >> On 26 Jul 2022, at 16:07, Cecil Westerhof via Python-list >> 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

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

2022-07-27 Thread Cecil Westerhof via Python-list
"Michael F. Stemper" 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

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

2022-07-27 Thread Chris Angelico
On Thu, 28 Jul 2022 at 05:36, Cecil Westerhof via Python-list wrote: > > Roel Schroeven writes: > > > Cecil Westerhof via Python-list schreef op 27/07/2022 om 17:43: > >> "Michael F. Stemper" writes: > >> > >> > This is orthogonal to your question, but might be of some use to you: > >> > > >> >

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

2022-07-27 Thread Alan Bawden
Cecil Westerhof writes: Alan Bawden writes: > Cecil Westerhof 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: >

Re: Simple TCP proxy

2022-07-27 Thread Chris Angelico
On Thu, 28 Jul 2022 at 04:32, Morten W. Petersen wrote: > > Hi Chris. > > You're thinking of the backlog argument of listen? Yes, precisely. > Well, STP will accept all connections, but can limit how many of the accepted > connections that are active at any given time. > > So when I bombed it

Re: Simple TCP proxy

2022-07-27 Thread Martin Di Paola
On Wed, Jul 27, 2022 at 08:32:31PM +0200, Morten W. Petersen wrote: You're thinking of the backlog argument of listen? From my understanding, yes, when you set up the "accepter" socket (the one that you use to listen and accept new connections), you can define the length of the queue for