c - 编程不同权重的随机分布

标签 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%)

最佳答案

像这样的东西应该可以工作,它是一个快速代码,所以可能会有一两个错误。请注意您所说的内容,尽管当前布局的可能性很小,但您可能会超过要分配的球数。例如: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 之间的随机值,如果它在 4 到 19 之间(请注意 else if)它将是绿色,否则蓝色的。分配球时也会发生同样的事情,我会在 0-7(8 个值)之间随机取一个值,然后将一个球加到一个上。

关于c - 编程不同权重的随机分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36075214/

相关文章:

algorithm - 通过折叠然后除以素数来散列 key ?

javascript - 如何使用 JQuery 执行嵌套的 for-each 循环以获取特定的 li 元素?

hardware - 简单的硬件RNG

c - 用C语言将整个程序写入一个文件

c - MapViewOfFile 向子进程发送整数

c - 将字符串输入到期望整数值的变量时无限循环

java - Alpha-beta 移动顺序

c - 如何访问另一个结构体内部的结构体

Python随机函数

c++ - 我需要访问一个随机的 .txt 文件,或者一个 .txt 文件的随机行,然后把它放到屏幕上