'python generate 2 numbers with max multiply result

I try generate 2 numbers with max multiply result 12.

I try:

height = random.randint(1, 12)
width = random.randint(1, 12//height)

The problem is that the probability of height being greater than width is higher.

How can I generate the height and width independently of each other?



Solution 1:[1]

EDIT: I think that you wanted the product to be less than 12 (not necessarily 12), but I'm still going to let this answer remain here for now.

If my answer doesn't help you, you could comment your problem in more detail and with more context of what you're trying to get.

I think you are trying to generate two random numbers, with their product 12.

I can think of multiple ways to achieve this, and this is one of them:

from random import choice # import choice

num = 12 # this number will control the product

factorChoice = choice([i for i in range(1,num+1) if num%i==0]) # selects a random factor of num

height = factorChoice # this is the height

width = 12//factorChoice # this is the width

print(f'Height: {height}\nWidth: {width}') # printing results

This is pretty inefficient, and looks even weirder, but hey— it works.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AashvikT