Percentage chance to make action

Question:

Simple problem:

percentage_chance = 0.36

if some_function(percentage_chance):
   # action here has 36% chance to execute
   pass

How can I write some_function, or an expression involving percentage_chance, in order to solve this problem?

Asked By: methyl

||

Answers:

You could use random.random:

import random

if random.random() < percentage_chance:
    print('aaa')
Answered By: SilentGhost
import random
if random.randint(0,100) < 36:
    do_stuff()
Answered By: Blue Peppers

This code returns a 1, 36% of the time

import random
import math
chance = 0.36
math.floor( random.uniform(0, 1/(1-chance)) )
Answered By: Vlad

Just to make it more explicitly clear and more readable:

def probably(chance):
    return random.random() < chance

if probably(35 / 100):
    do_the_thing()
Answered By: ramazan polat

Porositas dari reservoar diperkirakan memiliki Medan 0.2 dan variasi 0.0004.Jika porositas dipercaya terdistribusi secara normal.Hitung:
a.berapa probabilitas porositas yang ditemukan antara 0.18 dan 0.22
b.Jika batuan dengan porositas kurang dari 15% bukan batuan reservoar.Berapakaj probabilitas batuan pada suatu lokasi ada batuan reservoar
c.Berapakah standar deviasi yg diperlukan agar probabilitas pada bagian b adalah 50%
d.Dengan variansi sama, berapakah mean yang diharapkan agar probabilitas batuan reservoar (batuan dengan porositas lebih besar dari 15%) adalah 85%.

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.