##### Loops Loops Loops
 Loops Loops Loops Gilush Silly Frenchman Posts: 45 Threads: 14 Joined: Jun 2020 Reputation:   Feb-22-2021, 12:34 AM (This post was last modified: Feb-22-2021, 12:35 AM by Gilush.) Hey guys, I have this empty port list that I want to fill with 10 random ports in a pre-defined range with a random N step so i've managed to, somehow, come up with that code: ``` ports = [] i = 0 while i < 10: for p in range(5000, 65000, random.randint(1000, 10000)): print(p) ports.append(p) i += 1 if i >= 20: break```This totaly works and do the job I wanted it to do. My question is: are there any other ways to do it? if so, what are they? and if you can and there's a generator in your answer, save it for last :) Thank you! (loops!!!! i'm stuck in a loop!!) Reply deanhystad So-and-so of the Yard Posts: 2,565 Threads: 12 Joined: Feb 2020 Reputation: Feb-22-2021, 05:24 AM Your first port will always be 5000 and 5000 may appear multiple times in the list. You could have more than 10 ports in the list. The step is randomly chosen, but it is not a randomly sized step (groups of steps will be the same). This doesn't match your description of what you want to do at all. You can fix the length problem like this: ```ports = [] while len(ports) < 10: for p in range(5000, 65000, random.randint(1000, 10000)): ports.append(p) if len(ports) >= 10: break```This code still has the problem that 5000 will always be the first port and it may appear in the list multiple times. Would you rather have 10 random ports in the range 5000 to 65000? This is easily done: `ports = random.sample(range(5000, 65000), k=10)`If you want them in order: `ports = sorted(random.sample(range(5000, 65000), k=10))` Reply DeaD_EyE Da Bishop Posts: 1,693 Threads: 6 Joined: May 2017 Reputation: Feb-22-2021, 08:56 AM ```import random def random_int_range(num, start, end, step=1): """ Return a random integer sample of size `num` from `start` to `end` + 1 with given `step`. If too less results are in the range, an empty list is returned instead of throwing a `ValueError` """ try: return random.sample(range(start, end + 1, step), k=num) except ValueError: return []``` My code examples are always for Python >=3.6.0 Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! Reply Gilush Silly Frenchman Posts: 45 Threads: 14 Joined: Jun 2020 Reputation: Feb-22-2021, 01:03 PM (This post was last modified: Feb-22-2021, 01:05 PM by Gilush.) (Feb-22-2021, 05:24 AM)deanhystad Wrote: Your first port will always be 5000 and 5000 may appear multiple times in the list. You could have more than 10 ports in the list. The step is randomly chosen, but it is not a randomly sized step (groups of steps will be the same). This doesn't match your description of what you want to do at all. You can fix the length problem like this: ```ports = [] while len(ports) < 10: for p in range(5000, 65000, random.randint(1000, 10000)): ports.append(p) if len(ports) >= 10: break```This code still has the problem that 5000 will always be the first port and it may appear in the list multiple times. Would you rather have 10 random ports in the range 5000 to 65000? This is easily done: `ports = random.sample(range(5000, 65000), k=10)`If you want them in order: `ports = sorted(random.sample(range(5000, 65000), k=10))` Thank you for taking the time to notice that. I did noticed that as well but just before I logged in (I had to fix a list bug and then noticed the 5000 but I thought i'll get to it later.. thanks!) It's my first time reading about the random.sample option. what is the "k" stands for? this opens up alot more options for me so thanks again. Reply Posts: 5,778 Threads: 113 Joined: Sep 2016 Reputation: Feb-22-2021, 04:34 PM (Feb-22-2021, 01:03 PM)Gilush Wrote: It's my first time reading about the random.sample option. what is the "k" stands for?random.sample Basically it picks `k` unique random elements(sample) from a sequence. ```>>> import random >>> >>> random.sample(range(1, 10), 5) [5, 3, 1, 7, 9] >>> random.sample(range(1, 10), 5) [8, 9, 6, 1, 4] >>> random.sample(range(1, 10), 11) Traceback (most recent call last): File "", line 1, in File "C:\Python39\lib\random.py", line 363, in sample raise ValueError("Sample larger than population or is negative") ValueError: Sample larger than population or is negative```If doing it like this there will not be unique elements. ```>>> [random.randint(1, 10) for i in range(5)] [5, 8, 8, 9, 5] >>> [random.randint(1, 10) for i in range(5)] [9, 1, 3, 5, 5]``` Gilush likes this post Reply Gilush Silly Frenchman Posts: 45 Threads: 14 Joined: Jun 2020 Reputation: Feb-22-2021, 05:33 PM (Feb-22-2021, 04:34 PM)snippsat Wrote: (Feb-22-2021, 01:03 PM)Gilush Wrote: It's my first time reading about the random.sample option. what is the "k" stands for?random.sample Basically it picks `k` unique random elements(sample) from a sequence. ```>>> import random >>> >>> random.sample(range(1, 10), 5) [5, 3, 1, 7, 9] >>> random.sample(range(1, 10), 5) [8, 9, 6, 1, 4] >>> random.sample(range(1, 10), 11) Traceback (most recent call last): File "", line 1, in File "C:\Python39\lib\random.py", line 363, in sample raise ValueError("Sample larger than population or is negative") ValueError: Sample larger than population or is negative```If doing it like this there will not be unique elements. ```>>> [random.randint(1, 10) for i in range(5)] [5, 8, 8, 9, 5] >>> [random.randint(1, 10) for i in range(5)] [9, 1, 3, 5, 5]``` Thank you. That answer helps me understand generators better. Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Multiple loops Paulman 4 464 Oct-11-2021, 08:25 AM Last Post: Paulman Break out of nested loops muzikman 11 760 Sep-18-2021, 12:59 PM Last Post: muzikman Possibility to create Checkpoints in Loops? coder_sw99 2 466 Jul-22-2021, 12:42 PM Last Post: coder_sw99 Help please: WHILE LOOPS with BOOLEAN LOGIC rbulmerstlouis 4 707 Jun-20-2021, 02:32 PM Last Post: rbulmerstlouis Why does Python not use parenthesis to contain loops? davidorlow 3 786 Jun-09-2021, 06:33 AM Last Post: Gribouillis Why recursive function consumes more of processing time than loops? M83Linux 9 1,616 May-20-2021, 01:52 PM Last Post: DeaD_EyE Realization of a tensor with for loops Elda46 5 756 May-07-2021, 03:53 PM Last Post: Gribouillis For loops returning wrong output Nighthound21 1 720 May-03-2021, 03:43 AM Last Post: deanhystad For loops index rturus 5 848 Mar-23-2021, 03:34 PM Last Post: deanhystad How to break out of nested loops pace 11 1,919 Mar-03-2021, 06:25 PM Last Post: pace

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020