【发布时间】:2017-02-22 15:08:16
【问题描述】:
我正在尝试创建一个随机数生成器,范围从 1 到 1000 共 100 次,以在此格式的控制台应用程序和弹出式 Windows 消息框 (MessageBox.Show) 上实现以下结果:
- 100 个随机数,按从小到大的顺序排列
- 生成的偶数数量
- 生成的最小数
- 生成的最大数
- 数字范围
我很困惑创建数组来存储值并获取使用我在这里所拥有的生成的偶数数量,例如我的数组名称将是“array”并使用“n”存储数字
string [] array = {item};
string output = string.Join("\n", array);
MessageBox.Show(output)
这是我的代码,如何添加?
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace NumberGenerator
{
class Program
{
static void Main(string[] args)
{
Random number = new Random();
int min = int.MaxValue,
max = int.MinValue;
for (int counter = 0; counter < 100; counter++)
{
int n = number.Next(0, 999);
Console.WriteLine(n);
if (n < min)
min = n;
if (n > max)
max = n;
}
int range = min - max + 1;
string[] array = { "Minimum number is (min)" };
string output = string.Join("\n", array);
Console.WriteLine("Minimum number = {0}, Maximum number = {1}, Range = {2}", min, max, range);
MessageBox.Show(output);
}
}
}
【问题讨论】:
-
您要询问 1->1.000 的 100 个数字。这样,您可能有 10/% 的重复项。你担心这个吗?
-
@XristosK 不,这不是我关心的问题
-
如果你正在寻找一个真正的随机数,你应该使用 RNGCryptoServiceProvider 类来生成它们,而不是使用 Random 类
-
@J.Tuc 我只需要输出100个不同的数字,不需要那么独特,最好用这种方法
标签: c# arrays random console-application