首页 文章

用不同权重编程随机分布

提问于
浏览
1

我要说的第一件事就是这个问题可能有一个名字,我根本就不知道它的名字 .

说明:

在插槽之间随机分布有8个球槽和100个球 . 有3种不同类型的插槽:红色,绿色和蓝色 . 红色插槽类型必须至少有6个球,绿色15和蓝色无关紧要 .

除了每种不同颜色所需的量之外,还有可能有多个红色,绿色或蓝色的插槽,每个插槽都有相同比例的球进入它们 . 红色是4%,绿色是15%,蓝色是未选择的其余部分 .

所以随机建议一个序列这是一种可能性:

Slot 1 - Blue with 17 balls
Slot 2 - Green with 8 balls
Slot 3 - Green with 12 balls
Slot 4 - Red with 1 ball
Slot 5 - Blue with 33 balls
Slot 6 - Red with 7 balls
Slot 7 - Blue with 12 balls
Slot 8 - Green with 10 balls

请注意,所需的数量已经填满,并且还有一个以上的红色和绿色插槽,尽管它只需要一个(内部至少有总数量的球) .

我需要的是一个伪代码或任何语言的代码,显示如何在不同的插槽和不同的权重之间分配所有100个球 . 我一直在编程,但每3次跑,一次不能分发每一个球,它错过了一些 .

--EDIT:我用C#编写的代码草图(这只是彩色插槽生成):

int amountOfRedSlots = 0, amountOfGreenSlots = 0, amountOfBlueSlots = 0;
    int[] slotColors = new int[8]; //1 - red, 2 - green, 3 - blue;
    for(int i = 0; i < 8; i++)
    {
        int num = Random.Range(1, 101);
        if (num <= 4) //Spawn a redSlot
        {
            amountOfRedSlots++;
            slotColors[i] = 1;
        }
        else if (num <= 19) //4 numbers excluded from not being a redSlot and 15 as percentage to be green
        {
            amountOfGreenSlots++;
            slotColors[i] = 2;
        }
        else
        {
            amountOfBlueSlots++;
            slotColors[i] = 3;
        }
    }
    if (amountOfRedSlots < 1)
    {
        int rand = Random.Range(1, 9); //Choose a random slot to be red
        if (slotColors[rand] == 2)
        {
            amountOfGreenSlots--;
        } else amountOfBlueSlots--;
        slotColors[rand] = 1;
        amountOfRedSlots++;
    }
    if (amountOfGreenSlots < 1)
    {
        int rand;
        do
        {
            rand = Random.Range(1, 9);
        } while (slotColors[rand] == 1); //Choose a random slot to be green, but it can't be a former red slot
        amountOfBlueSlots--; //Since there isn't a greenSlot, and we made sure it wasn't red, its certainly a former blue slot
        slotColors[rand] = 2;
        amountOfGreenSlots++;
    }

    //Now its needed to distribute the balls between the slots, giving the required minimum amount to be inside red slots and green slots
    //Also note that there is smaller chance of a ball going inside a red/green slot (4% and 15%)

1 回答

  • 0

    像这样的东西应该工作它是一个快速的代码,所以可能有一两个错误 . 请注意您所说的内容,尽管当前布局存在的可能性很小,但您可能会超出要分配的球数 . 例如:6绿色和2红色 .

    这是代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    enum types{RED=1,BLUE,GREEN};
    
    typedef struct 
    {
        int color;
        unsigned int number_balls;
    }slot_t;
    
    
    #define NUM_SLOTS 8
    #define GREEN_PROBABILITY 15
    #define RED_PROBABILITY 4
    
    #define MINIMUM_RED 6
    #define MAX_NUMBER_BALLS 100
    int main(void)
    {
        slot_t slots[NUM_SLOTS];
        int slots_created,random_number;
        int ball_count=0;
        srand(time(NULL));
        for(slots_created=0;slots_created<NUM_SLOTS;slots_created++ )
        {
            random_number=rand()%100+1;//random value between 1-100
            if(random_number<=RED_PROBABILITY){
                slots[slots_created].color=RED;
                slots[slots_created].number_balls=MINIMUM_RED;
                ball_count+=MINIMUM_RED;
    
            }
            else if(random_number<=GREEN_PROBABILITY+RED_PROBABILITY)
            {
                slots[slots_created].color=GREEN;
                slots[slots_created].number_balls=MINIMUM_RED;
                ball_count+=MINIMUM_RED;
            }
            else if(random_number<=GREEN_PROBABILITY)
                ;//blue case
        }   
    
        while(ball_count<MAX_NUMBER_BALLS)
        {
            slots[rand()%NUM_SLOTS].number_balls+=1; //add one ball
            ++ball_count;
        }
    
    
    }
    

    如果数字在1-4之间(4%的可能性为红色),我会在1-100之间获取一个randome值,如果它在4到19之间(注意其他的话),它将是绿色,否则为蓝色 . 当分配时,球会发生同样的事情,我 grab 0-7(8个值)之间的随机值并将球添加到一个 .

相关问题