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); } } }
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));
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(); } } }
在[0 - a)为A,[a - a+b)为B,[a+b - 1)为C。
这是两道不同的题。
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);
}
}
}
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));
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();
}
}
}