Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可以在c#构造函数中创建一个随机整数列表吗?_C#_.net_Random - Fatal编程技术网

可以在c#构造函数中创建一个随机整数列表吗?

可以在c#构造函数中创建一个随机整数列表吗?,c#,.net,random,C#,.net,Random,我创建了一个构造函数,最初可以创建一些硬编码的值。现在是我需要随机创建值的时候了。是否可以在构造函数中执行此操作。例如,我有以下几点: private Random _random; public GuessingGame() { this.Guesses = new List<Guess>(); this.Target = new List<int>() { 1, 2, 3 }; this._random = new Random(); }

我创建了一个构造函数,最初可以创建一些硬编码的值。现在是我需要随机创建值的时候了。是否可以在构造函数中执行此操作。例如,我有以下几点:

private Random _random;

public GuessingGame()
{
    this.Guesses = new List<Guess>();
    this.Target = new List<int>() { 1, 2, 3 };
    this._random = new Random();
}

public List<int> Target { get; set; }
public List<Guess> Guesses { get; set; }
private Random\u Random;
公众猜谜游戏
{
this.guesss=新列表();
this.Target=newlist(){1,2,3};
这个._random=new random();
}
公共列表目标{get;set;}
公共列表猜测{get;set;}
我尝试创建一个
新的Random()
,然后尝试分配它,但没有成功。在构造函数中创建随机整数的最佳方法是什么?

这行代码

for(int i=0;i<10;i++)
{
 this.Target.Add(this._random.Next(0,10));
}
for(int i=0;i
public gussinggame()
{
this.guesss=新列表();
这个._random=new random();
this.Target=新列表();
int randomCount=3;//有多少个随机数
int rndMin=1;//随机变量的最小值
int rndMax=10;//随机变量的最大值
对于(int i=0;i
LINQ版本:

public GuessingGame() {
    _random = new Random();
    Target = Enumerable
                 .Repeat(0, how_many_items)
                 .Select(x => _random.Next(max_random_number_value))
                 .ToList();
    // ..the rest..
}

什么不起作用?任何异常?只需调用_random.Next in a loop所需的次数,并将值添加到列表中可能更紧凑的方式创建3个随机值的列表
Target=Enumerable。重复(1,3)。选择(i=>u random.Next()).ToList()
public GuessingGame()
{
    this.Guesses = new List<Guess>();
    this._random = new Random();
    this.Target = new List<int>();

    int randomCount = 3; // how many randoms
    int rndMin = 1; // min value of random
    int rndMax = 10; // max value of random


    for (int i = 0; i < randomCount; i++)
      this.Target.Add(this._random.Next(rndMin, rndMax));
} 
public GuessingGame() {
    _random = new Random();
    Target = Enumerable
                 .Repeat(0, how_many_items)
                 .Select(x => _random.Next(max_random_number_value))
                 .ToList();
    // ..the rest..
}