Python Forum

Full Version: [split] why can't i create a list of numbers (ints) with random.randrange()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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...
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.
okay thank you,
i'll get more into it tomorrow,
i'm going to sleep...

thank you...
(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.
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!
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.
(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...
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)