0

I'm trying to write a line of code that will allow me to create a random list of 5 values, whose lengths I can vary in size. For example, I want the numbers in the list to have a length of 2 or 3 integers, like this: [11, 34, 67, 88, 93] or [100, 450, 622, 875, 998].

So far I have this:

import random mylist = [] for i in range(5): p=p+[random.randrange(?????) print(p) 

Can anyone help me as I've been trying to do this all day and it's driving me crazy! Thanks.

2
  • Do you mean 2 or 3 digits? i.e. 10-99 vs 100-999? And is that criteria random for each list, or specified elsewhere? Commented Jan 29, 2016 at 19:35
  • Can they have both 2 and 3 digits like [10, 50, 213, 567]? Commented Jan 29, 2016 at 19:42

4 Answers 4

2
n_digits = 3 min_num = 10**(n_digits-1) max_number = 10**n_digits -1 #thanks @TomKarzes size_of_list = 5 my_list = [random.randint(min_num,max_num) for i in range(size_of_list)] 
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry Joran, but could I please direct your attention to our chat room?
1

Use sample to take arbitrary value( here 5) from a list

>>>import random >>>random.sample(range(10000),5)# or below >>>random.sample(range(10,999),5)#2-3 integer numbers range >>>random.sample(range(10,99),5)#2 integer numbers range >>>random.sample(range(100,999),5)#3 integer numbers range 

3 Comments

random.sample(range(10, 1000), 5)
@user3100115 what do you mean?
The question is ...create a random list of 5 values then ...I want the numbers in the list to have a length of 2 or 3 integers
1
import random mylist = [] for i in range(5): m = random.choice([0,1]) mylist.append(random.randrange(10*m, (100*m)+1)) print(mylist) 

1 Comment

it took me a while but now I see how you interpreted the OP question ... cool answer assuming you interpreted it right
0
import random choices = [(10, 99), (100,999)] bounds = random.choice(choices) mylist = [random.randint(*bounds) for i in range(5)] print mylist 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.