Home > Net >  How to increase the probability of choosing a number?
How to increase the probability of choosing a number?

Time:01-03

So i have a code that choses a random number between 1 and 2. If the code choses 1, i want to increase the probability of choosing 1 by 10%. If the code choses 2, i want to increase the probability of choosing by 2 15%. Here is my code :


import pygame, random,sys

pygame.init()
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 500
BLACK = (0,0,0)
white = "#FFFFFF"
win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Test")

def test2():
win.fill(BLACK)

    test = random.randint(1,2) 
    text_font = pygame.font.Font('freesansbold.ttf', 100)
    screws_text = text_font.render(str(test), True ,white)
    textRect = screws_text.get_rect()
    textRect.center = (250,250)
    win.blit(screws_text, textRect)
    
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit() 
    pygame.display.update()

while True:

    for event in pygame.event.get():
        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                test2()
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

I didn't really tried anything because i have no idea that i should do, i never did that so i would be very grateful if you could help me.

CodePudding user response:

I suggest to use random.random() which returns a floating point value in the range [0.0, 1.0]. Choose one of the two numbers depending on a threshold that is calculated from the probability:

probability_1 = 1
probability_2 = 1
def test2():
    global probability_1, probability_2 

    threshold = probability_1 / (probability_1   probability_2)
    test = 1 if random.random() < threshold else 2
    if test == 1:
        probability_1  = 0.1
    else:
        probability_2  = 0.15

CodePudding user response:

The choice function from the NumPy library lends the possibility to define the weights of each possible candidate as a list. This way, you can define a 50/50 chance initially, and modify with the call of a function, like this:

from numpy.random import choice

def choiceChanger(candidates, weights):
    draw = choice(candidates, 1, weights)
    if draw == 1:
        weights[1]  = 0.05
        weights[2] -= 0.05
    else:
        weights[1] -= 0.075
        weights[2]  = 0.075

    return weights

candidates = [1,2]
weightsDict = {1: 0.5, 2:0.5}
weights = [weightsDict[e] for e in weightsDict.keys()]

weights = choiceChanger(candidates, 1, weights)

I also think this way it's way easier to generalize, if you need to change the probabilities of more than two numbers: you'll just have to edit the function instead of the whole program.

The math of adding '0.05' instead of '0.1' is so the overall probability is 1. Same thing with '0.075', it's just the probability you want to add (15%) distributed across the possible choices (in this case 2) so 15% divided by 2.

  • Related