我正在尝试按c#顺序按最高编号并以逗号分隔生成五组随机数
我尝试使用
for(let a = 0; a < node.children.length; a++) {
node.children[a].id = "my-new-id";
}
我只得到一组输出而不进行排序(从高到低),即
随机数是:12、2、12、19、11
预期输出应如下::
答案 0 :(得分:3)
只需增强您的代码即可。
class Program
{
static void Main(string[] args)
{
int[] randNumber = new int[5];
Random rand = new Random();
Console.WriteLine("The random numbers are: ");
for (int i = 0; i < 5; i++)
{
for (int h = 0; h < randNumber.Length; h++)
{
randNumber[h] = rand.Next(1, 20);
}
randNumber = randNumber.OrderByDescending(x => x).ToArray();
Console.WriteLine(string.Join(", ", randNumber));
}
}
}
答案 1 :(得分:3)
另一个...
static void Main(string[] args)
{
Random R = new Random();
List<List<int>> sets = new List<List<int>>();
for(int s=1; s<=5; s++)
{
List<int> set = new List<int>();
for (int i = 1; i <= 5; i++)
{
set.Add(R.Next(1, 20));
}
set.Sort();
set.Reverse();
sets.Add(set);
}
for(int i=1; i<=sets.Count; i++)
{
Console.WriteLine(i.ToString() + ". " + string.Join(", ", sets[i-1]));
}
Console.Write("Press Enter to quit");
Console.ReadLine();
}
示例输出:
1. 19, 18, 12, 9, 3
2. 18, 17, 15, 5, 3
3. 18, 11, 9, 7, 1
4. 13, 12, 10, 10, 6
5. 12, 6, 5, 5, 3
Press Enter to quit
答案 2 :(得分:2)
这里是一种方法:
var rnd = new Random();
Console.WriteLine(
string.Join("\n",
Enumerable
.Range(1, 5) // returns an IEnumerable<int> containing 1,2,3,4,5.
.Select(s => // for each int in the IEnumerable
string.Join(",",
Enumerable
.Range(1, 5)
.Select(i => rnd.Next(1, 20)) // get a random number.
.OrderByDescending(r => r) // order the inner IEnumerable
)
)
)
);
结果(好吧,无论如何可能的结果):
17,17,10,2,1
19,10,10,8,2
17,16,9,3,3
17,16,8,5,1
18,14,13,10,3