-2
def rng():
    return randint(1, 100)

is

if rng() <= x and rng() <= y and rng() <= z:
   .....

The same as is

if rng() <= x and y and z:
   .....

What im trying to do is take a number from the function rng() then use it to compare with the variables inside the if statement

and i'm questioning whether on the first version, the rng() is ran 3 times making it 3 different numbers for each compare and not the same number.

or the second version can work

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
BoomPow
  • 1
  • 4

1 Answers1

1

As @jonsharpe indicated, neither of those would work. The second one would be nonsensical (that isn't how and works) while the first would make more sense but still fail. It would fail because it will compute three different random numbers and not a single one. You would need to do something like:

r = rng()
if r <= x and r <= y and r <= z:
    #do something

In this special case, you could also use the min operator:

if rng() <= min(x,y,z):
    #do something

although obviously not all comparisons involving r can be reduced to just 1.

John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • You should probably also use `r = rng()` in the second case, using `min` – tobias_k Sep 13 '19 at 11:11
  • 1
    @tobias_k it depends on what OP is doing this for. If the point is to have the code in the body of the `if` execute with a specific probability which depends on `x,y,z,` and there is no further role for `r` to play, then defining `r` at all would be superfluous. – John Coleman Sep 13 '19 at 11:16