1

I am using the random library in python to select win or lose.

import random

choice = random.choice(['win','lose'])

Is there anyway I can say set the probability in the code to say I want more lose than win everytime the code runs?

RustyShackleford
  • 3,462
  • 9
  • 40
  • 81

2 Answers2

2

Well, @CloC is technically right, you could do that, but better use the standard Python library as it is designed, less code, less bugs

F.e., sampling with probability of Win being 60%

v = random.choices(["Win", "Lose"], weights=[60, 40], k=1)
Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
1

A way to control random (which wont be random...) would be to:

Generate a number between 1 and 100:

n = random.randint(1,101)

and then compare it with the percentage of win/loses you want :

chances_to_win = 51  # 50%

if n < chances_to_win:
    print(str(chances_to_win-1) +"% chances to Win")
    choice = "Win"
else:
    print(str(100-(chances_to_win-1)) +"% chances to Lose")
    choice = "Lose"

That way, you can control what is the percentage of wins and loses.

SivolcC
  • 3,258
  • 2
  • 14
  • 32