问个算法问题:有三个字母A,B,C 想通过设置三个概率值比如:0.5,0.2,0.3(相加概率为1),概率的产生100次ABC三个字母,求思路!!!

解决方案 »

  1.   

    先生成50个A、20个B、30个C,随机抽取...
      

  2.   

    产生这些比率的最小公倍数即可。10000还是几千无所谓。另外还可以直接产生0~1的随机数。
    在[0 - a)为A,[a - a+b)为B,[a+b - 1)为C。
      

  3.   

    真正的概率,即使你连续99次抽到了A,第一百次取到A的概率仍是0.5.楼主首先要清楚,你是要真正按概率取,还是希望取到50个A、20个B、30个C?
    这是两道不同的题。
      

  4.   

    就像8楼所说的,随即一个0-9之间的数,小于5就是a,小于7就是b,剩下是C
      

  5.   

    取随即数,取值为0-9的数。小于等于5就是A,大于5且小于等于7就是B,大于7且小于等于9为C。
      

  6.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace RandomGenerator
    {
        class Program
        {
            const int LEN = 100;
            const int SIZE = 1000;
            const double probA = 0.5;
            const double probB = 0.2;                static void Main(string[] args)
            {
                char[] numberList = new char[LEN];
                Random Rand = new Random((int)DateTime.Now.Ticks);            int num;
                int aThres = (int)(probA * SIZE);
                int bThres = (int)((probA + probB) * SIZE);            // generate the list
                for (int i = 0; i < LEN; i++)
                {
                    num = Rand.Next(1, SIZE);                if (num <= aThres)
                    {
                        numberList[i] = 'A';
                    }
                    else if (num <= bThres)
                    {
                        numberList[i] = 'B';
                    }
                    else
                    {
                        numberList[i] = 'C';
                    }
                    Console.Write("{0}\t", numberList[i]);
                }
                Console.WriteLine();            // check result
                int aNum = 0, bNum = 0, cNum = 0;
                for (int i = 0; i < LEN; i++)
                {
                    if(numberList[i] == 'A')
                    {
                        aNum++;
                    }
                    else if (numberList[i] == 'B')
                    {
                        bNum++;
                    }
                    else
                    {
                        cNum++;
                    }
                }
                Console.WriteLine("A: {0}, B: {1}, C: {2}", aNum, bNum, cNum);
            }
        }
    }
      

  7.   

    Random ran = new Random();
                double a = 0.5;
                double b = 0.2;
                double c = 0.3;
                int aIndex = 0;
                int bIndex = 0;
                int cIndex = 0;
                for (int i = 0; i < 100; i++)
                {
                    double next = ran.NextDouble();
                    if (next <= a)
                    {
                        aIndex++;
                    }
                    else if (a < next && next <= a + b)
                    {
                        bIndex++;
                    }
                    else
                    {
                        cIndex++;
                    }
                }
                MessageBox.Show(string.Format("A出现{0}次,B出现{1}次,C出现{2}次。",aIndex,bIndex,cIndex));
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;namespace Example
    {
        class Program
        {
            static void Main(string[] args)
            {
                char[] ch={'A','B','C'};
                int item;
                int[] count=new int[3]{0,0,0};
                Random random = new Random(Guid.NewGuid().GetHashCode());
                for (int i = 0; i < 100;i++ )
                {
                    int r=random.Next(1, 11);
                    if ( r<= 5)
                    {
                        item = 0;
                        count[item]++;
                        Console.Write(ch[item]);
                    }
                    else if (r<=7)
                    {
                        item = 1;
                        count[item]++;
                        Console.Write(ch[item]);
                    }
                    else
                    {
                        item = 2;
                        count[item]++;
                        Console.Write(ch[item]);
                    }
                }            Console.WriteLine("A出现的次数{0},B出现的次数{1},C出现的次数{2}", count[0], count[1], count[2]);
                Console.ReadLine();
            }
        }
    }