用不同权重编程随机分布

时间:2016-03-18 02:40:56

标签: c algorithm random pseudocode

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

解释:

在插槽之间有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次跑,一次不能分发每一个球,它错过了一些。

- 编辑: 我在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 :(得分:0)

这样的东西应该有用,它是一个快速的代码,所以可能有一两个bug。请注意您所说的内容,尽管当前布局存在的可能性很小,但您可能会超出要分配的球数。例如: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%的可能性为红色),如果它在4到19之间,我会抓住1-100之间的randome值(注意否则如果)它会是绿色的,否则是蓝色的。球在分布时会发生同样的事情,我会抓住0-7(8个值)之间的随机值并将球添加到一个球中。