Python Forum
Loops Loops Loops - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Loops Loops Loops (/thread-32618.html)



Loops Loops Loops - Gilush - Feb-22-2021

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!!)


RE: Loops Loops Loops - deanhystad - Feb-22-2021

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))



RE: Loops Loops Loops - DeaD_EyE - Feb-22-2021

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 []



RE: Loops Loops Loops - Gilush - Feb-22-2021

(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.


RE: Loops Loops Loops - snippsat - Feb-22-2021

(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 "<interactive input>", line 1, in <module>
  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]



RE: Loops Loops Loops - Gilush - Feb-22-2021

(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 "<interactive input>", line 1, in <module>
  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.