程序生成50个随机数(1到10之间),并告知小于5且大于5的值(C#)

时间:2017-03-01 13:36:03

标签: c# if-statement random visual-studio-2015 numbers

程序生成50个随机数(介于1到10之间)并告知小于5且大于5的值(C#)

2 个答案:

答案 0 :(得分:1)

试试这个:

int n = 50
IList<int> randomNumbers = new List<int>(n);
Random ran = new Random(1);

for (int i = 0; i < n; i++)
{
     randomNumbers.Add(ran.Next(1, 10));
}

int lessThan5Count = randomNumbers.Count(c => c < 5);
int greaterThan5Count = randomNumbers.Count(c => c > 5);     

答案 1 :(得分:0)

你可以这样做:

Random r = new Random();
int n = 50;
int smaller_than_5 = 0;
int bigger_than_5 = 0;
double[] d = new double[n];
for (int i = 0; i < n; i++)
{
    d[i] = 1 + r.NextDouble() * 9;
    if (d[i] < 5) smaller_than_5++;
    else if (d[i] > 5) bigger_than_5++;
}

希望这会有所帮助......