gpt4 book ai didi

c# - 通过生成包含数字的随机任务来添加数字

转载 作者:行者123 更新时间:2023-11-30 20:43:43 24 4
gpt4 key购买 nike

<分区>

public TestClass{

public Task<int> GetRandomNumber() {
return Task.FromResult(new Random().Next(0, 1500));
}
}



public class Default
{
static void Main(string[] args)
{
var test = new TestClass();
List<int> adddata = new List<int>();
for (int i = 0; i < 3; i++)
{
var result = Task.Run(() => test.GetRandomNumber());
Console.WriteLine("The values that will be added are :{0}", result.Result);
adddata.Add(result.Result);
}
Console.WriteLine("The value is :{0}", adddata.Sum(v => v));
}
}

问题是随机数返回相同的数字,68、68、122,而它应该返回不同的数字我做错了什么我正在尝试了解如何在 C# 中使用任务。谢谢!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com