-1

For example

a = "a"
b = "b"
c = "c"
d = "d"

taking these variables and choosing a random one

a = "a"
b = "b"

taking these variables, at a 75% chance choosing a and at a 25% chance choosing b

  • Possibly you're looking for something like https://stackoverflow.com/q/3679694/3001761, but it's not clear to me. – jonrsharpe Jan 19 '21 at 14:29

1 Answers1

-1

You can add weights to random.choices This is how I did it:

import random

lst= ["a", "b"]
print(random.choices(lst, weights=(0.75, 0.25), k=1))

k = 1 represents the amount of values to choose from the list, which, in our case, is only 1.

The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24
  • 2
    **the weights MUST add up to 1**, `random.choices([1, 2, 3], weights=[100, 100, 100])` does not throw any error. – Countour-Integral Jan 19 '21 at 14:31
  • I've learned probability this way. If you've learned it a different way, then feel free to use your way. – The Pilot Dude Jan 19 '21 at 14:32
  • 2
    The point is more that you seem to be making claims about the API that aren't true. [`random.choices`](https://docs.python.org/3/library/random.html#random.choices) **does not** require floats that add to 1, because it will normalise them to that internally anyway. – jonrsharpe Jan 19 '21 at 14:34
  • If it makes you happy, then fine, I'll change it – The Pilot Dude Jan 19 '21 at 14:36
  • 2
    That's not a particularly constructive attitude. It's not about anyone's happiness; we're building a collection of high-quality technical information, it needs to be *correct*. This is also reflected in the edits that have repeatedly been applied to your posts to remove *"Hope this helps!"*, please do learn from those. – jonrsharpe Jan 19 '21 at 14:50
  • Apologies. I was simply **trying** to be nice – The Pilot Dude Jan 19 '21 at 14:55