Python Forum
[split] why can't i create a list of numbers (ints) with random.randrange()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] why can't i create a list of numbers (ints) with random.randrange()
#1
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...
Yoriz write Oct-21-2022, 07:59 PM:
Your new separate question has been split into its own thread, please create new threads for future further questions.
Reply
#2
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.
Reply
#3
okay thank you,
i'll get more into it tomorrow,
i'm going to sleep...

thank you...
Reply
#4
(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.
Reply
#5
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!
Reply
#6
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.
Reply
#7
(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...
Reply
#8
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,720 Jan-05-2024, 08:30 PM
Last Post: sgrey
  random numbers, randint janeik 2 574 Nov-27-2023, 05:17 PM
Last Post: janeik
  Sample random, unique string pairs from a list without repetitions walterwhite 1 464 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,554 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,255 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  List of random numbers astral_travel 17 2,725 Dec-02-2022, 10:37 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,346 Nov-13-2022, 01:27 AM
Last Post: menator01
  Split string using variable found in a list japo85 2 1,309 Jul-11-2022, 08:52 AM
Last Post: japo85
  Divide a number by numbers in a list. Wallen 7 8,059 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  how to easily create a list of already existing item CompleteNewb 15 3,573 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb

Forum Jump:

User Panel Messages

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