Home > Software design >  How can i create a list of 50 random numbers where the even numbers are twice the odd numbers?
How can i create a list of 50 random numbers where the even numbers are twice the odd numbers?

Time:08-20

can you help me with this problem please? I have to create a list of 50 random numbers from 1 to 10 where the odd numbers frequency is aproximately twice the even number frequency.

CodePudding user response:

Try two list, one with 33 random odd numbers and the other with 17 random even numbers, and then merge the two lists.

CodePudding user response:

Solution as simplified

import random
odd=[item for item in range(1,11,2)]
even=[item for item in range(2,11,2)]
# twice chance 50/3 ~17   odd=33  even=17
random_even=[random.choice(even) for _ in range(17)]
random_odd=[random.choice(odd) for _ in range(33)]
random_number=random_even random_odd # sum 
random.shuffle(random_number) # messing up order
print("even",len(random_even),"odd",len(random_odd)) #check len
print("len list  : ",len(random_number))#check len
print(random_number)#show list

  • Related