Posts: 230 Threads: 39 Joined: Mar 2020 Oct-21-2022, 04:08 PM (This post was last modified: Oct-21-2022, 07:59 PM by Yoriz.) hey, why can't i create a list of numbers (integers) with the random.randrange() method as in: print(random.randrange(0,100,2)) or random.sample() or randint() i thought it suppose to generate a list... Posts: 6,920 Threads: 22 Joined: Feb 2020 Oct-21-2022, 04:35 PM (This post was last modified: Oct-22-2022, 12:44 PM by deanhystad.) randrange(start, stop[, step]) returns a single value, not a list. It is like doing this: random.choice(range(start, stop, step)) You could select a list using random.sample() or random.choices(). random.choices can have repeats (like calling random.randrange multiple times): values = random.choices(range(start, stop, step), k=count) random.sample looks the same, but k must be less than the number of values in range(start, stop, step), and there are no repeated values. Posts: 230 Threads: 39 Joined: Mar 2020 okay thank you, i'll get more into it tomorrow, i'm going to sleep... thank you... Posts: 1,835 Threads: 2 Joined: Apr 2017 (Oct-21-2022, 04:08 PM)astral_travel Wrote: i thought it suppose to generate a list... Make sure to check documentation to confirm what you think: https://docs.python.org/3/library/random....randrange. Posts: 1,212 Threads: 146 Joined: Jul 2017 Just some fun on Sunday morning! import random x = random.randint(1, 10) print(x) # 1 even random number coming up! x = random.randrange(0,20,2) print(x) # generate a load of even random numbers for x in range(0, 11): print('x is', x) print('random number is', random.randrange(0, 20, 2)) # 1 list of even random numbers coming up! mylist = [random.randrange(0, 20, 2) for i in range(11)] # 1 list of odd random numbers coming up! mylist = [random.randrange(1, 20, 2) for i in range(11)] # 1 random binary number coming up! mylist = [str(random.randrange(0, 2)) for i in range(11)] random_binary_string = ''.join(mylist) # probably not good if it starts with zero if rbs[0] == '0': binary = rbs[1:] else: binary = rbs # How to convert this binary type string to a binary number?? # I don't know! Posts: 6,920 Threads: 22 Joined: Feb 2020 Oct-23-2022, 04:36 PM (This post was last modified: Oct-23-2022, 04:36 PM by deanhystad.) This does not work to remove leading zeros from a binary string. # probably not good if it starts with zero if rbs[0] == '0': binary = rbs[1:] else: binary = rbs It works for "011001" but not "0011001". For a more generic solution use lstrip("0"). Use int(binary_string, 2) to convert a binary string to a binary number. Posts: 230 Threads: 39 Joined: Mar 2020 Oct-23-2022, 07:17 PM (This post was last modified: Oct-23-2022, 07:17 PM by astral_travel.) (Oct-21-2022, 04:35 PM)deanhystad Wrote: randrange(start, stop[, step]) returns a single value, not a list. It is like doing this: random.choice(range(start, stop, step)) You could select a list using random.sample() or random.choices(). random.choices can have repeats (like calling random.randrange multiple times): values = random.choices(range(start, stop, step), k=count) random.sample looks the same, but k must be less than the number of values in range(start, stop, step), and there are no repeated values. thank you Dean, works like magic... and thank you ndc85430 for the link... Posts: 1,212 Threads: 146 Joined: Jul 2017 Ah well, I was only thinking about the number I got, which only had 1 leading zero! # 1 random binary number coming up! mylist = [str(random.randrange(0, 2)) for i in range(11)] while mylist[0] == '0': del mylist[0] rbs = ''.join(mylist) |