Escribe una función que genere uno de 3 números según las probabilidades dadas

Tienes una función rand(a, b) que genera números aleatorios equiprobables entre [a, b] inclusive. Genere 3 números x, y, z con probabilidad P(x), P(y), P(z) tales que P(x) + P(y) + P(z) = 1 usando el rand(a,b) dado ) función.
La idea es utilizar la característica equiprobable del rand(a,b) provisto. Sean las probabilidades dadas en forma de porcentaje, por ejemplo P(x)=40%, P(y)=25%, P(z)=35%. .

Los siguientes son los pasos detallados. 
1) Genera un número aleatorio entre 1 y 100. Como son equiprobables, la probabilidad de que salga cada número es 1/100. 
2) Los siguientes son algunos puntos importantes a tener en cuenta sobre el número aleatorio generado ‘r’. 
a) ‘r’ es menor o igual que P(x) con probabilidad P(x)/100. 
b) ‘r’ es mayor que P(x) y menor o igual que P(x) + P(y) con P(y)/100. 
c) ‘r’ es mayor que P(x) + P(y) y menor o igual que 100 (o P(x) + P(y) + P(z)) con probabilidad P(z)/100.
 

C

// This function generates 'x' with probability px/100, 'y' with
// probability py/100  and 'z' with probability pz/100:
// Assumption: px + py + pz = 100 where px, py and pz lie
// between 0 to 100
int random(int x, int y, int z, int px, int py, int pz)
{      
        // Generate a number from 1 to 100
        int r = rand(1, 100);
      
        // r is smaller than px with probability px/100
        if (r <= px)
            return x;
 
         // r is greater than px and smaller than or equal to px+py
         // with probability py/100
        if (r <= (px+py))
            return y;
 
         // r is greater than px+py and smaller than or equal to 100
         // with probability pz/100
        else
            return z;
}

Java

// This function generates 'x' with probability px/100, 'y'
// with probability py/100  and 'z' with probability pz/100:
// Assumption: px + py + pz = 100 where px, py and pz lie
// between 0 to 100
static int random(int x, int y, int z, int px, int py,
                  int pz)
{
    // Generate a number from 1 to 100
    int r = (int)(Math.random() * 100);
 
    // r is smaller than px with probability px/100
    if (r <= px)
        return x;
 
    // r is greater than px and smaller than or equal to
    // px+py with probability py/100
    if (r <= (px + py))
        return y;
 
    // r is greater than px+py and smaller than or equal to
    // 100 with probability pz/100
    else
        return z;
}
 
// This code is contributed by subhammahato348.

Python3

import random
 
# This function generates 'x' with probability px/100, 'y' with
# probability py/100  and 'z' with probability pz/100:
# Assumption: px + py + pz = 100 where px, py and pz lie
# between 0 to 100
def random(x, y, z, px, py, pz): 
     
    # Generate a number from 1 to 100
    r = random.randint(1, 100)
     
    #  r is smaller than px with probability px/100
    if (r <= px):
        return x
     
    # r is greater than px and smaller than
    # or equal to px+py with probability py/100
    if (r <= (px+py)):
        return y
         
    # r is greater than px+py and smaller than
    # or equal to 100 with probability pz/100
    else:
        return z
     
# This code is contributed by rohan07

C#

// This function generates 'x' with probability px/100, 'y'
// with probability py/100  and 'z' with probability pz/100:
// Assumption: px + py + pz = 100 where px, py and pz lie
// between 0 to 100
static int random(int x, int y, int z, int px, int py,
                  int pz)
{
    // Generate a number from 1 to 100
    Random rInt = new Random();
    int r = rInt.Next(0, 100);
 
    // r is smaller than px with probability px/100
    if (r <= px)
        return x;
 
    // r is greater than px and smaller than or equal to
    // px+py with probability py/100
    if (r <= (px + py))
        return y;
 
    // r is greater than px+py and smaller than or equal to
    // 100 with probability pz/100
    else
        return z;
}
 
// This code is contributed by subhammahato348.

Javascript

// This function generates 'x' with probability px/100, 'y' with
// probability py/100  and 'z' with probability pz/100:
// Assumption: px + py + pz = 100 where px, py and pz lie
// between 0 to 100
function random(x, y, z, px, py, pz)
{      
        // Generate a number from 1 to 100
        let r = Math.floor(Math.random() * 100) + 1;
      
        // r is smaller than px with probability px/100
        if (r <= px)
            return x;
 
         // r is greater than px and smaller than or equal to px+py
         // with probability py/100
        if (r <= (px+py))
            return y;
 
         // r is greater than px+py and smaller than or equal to 100
         // with probability pz/100
        else
            return z;
}
 
// This code is contributed by subhammahato348.

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)

Esta función resolverá el propósito de generar 3 números con tres probabilidades dadas.
Este artículo es una contribución de Harsh Agarwal . Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *