C# 如何生成不带';I don’我不会一直在';运行';多少人?

C# 如何生成不带';I don’我不会一直在';运行';多少人?,c#,.net,random,C#,.net,Random,您好,我编码了这个OneAtRandom()扩展方法: public static class GenericIListExtensions { public static T OneAtRandom<T>(this IList<T> list) { list.ThrowIfNull("list"); if (list.Count == 0) throw new ArgumentEx

您好,我编码了这个OneAtRandom()扩展方法:

public static class GenericIListExtensions
   {
      public static T OneAtRandom<T>(this IList<T> list)
      {
         list.ThrowIfNull("list");
         if (list.Count == 0)
            throw new ArgumentException("OneAtRandom() cannot be called on 'list' with 0 elements");

         int randomIdx = new Random().Next(list.Count);
         return list[randomIdx];
      }
}
公共静态类GenericListExtensions
{
公共静态T OneAtRandom(此IList列表)
{
列表。ThrowIfNull(“列表”);
如果(list.Count==0)
抛出新ArgumentException(“无法对包含0个元素的“列表”调用OneAtRandom());
int randomIdx=new Random().Next(list.Count);
返回列表[randomIdx];
}
}
使用此单元测试测试失败:

[Test]
        public void ShouldNotAlwaysReturnTheSameValueIfOneAtRandomCalledOnListOfLengthTwo()
        {
            int SomeStatisticallyUnlikelyNumberOf = 100;
            IList<string> list = new List<string>() { FirstString, SecondString };
            bool firstStringFound = false;
            bool secondStringFound = false;

            SomeStatisticallyUnlikelyNumberOf.Times(() =>
            {
                string theString = list.OneAtRandom();
                if (theString == FirstString) firstStringFound = true;
                if (theString == SecondString) secondStringFound = true;
            });
            Assert.That(firstStringFound && secondStringFound);
        }
[测试]
public void不应始终将相同的价值提交给LandMcAlledon Longthow()列表
{
int-SomeStatisticallyUnlikelyNumberOf=100;
IList list=new list(){FirstString,SecondString};
bool firstStringFound=false;
bool secondStringFound=false;
有些统计结果不太可能是次数(()=>
{
字符串theString=list.OneAtRandom();
如果(theString==FirstString)firstStringFound=true;
如果(theString==SecondString)secondStringFound=true;
});
Assert.That(firstStringFound&&secondStringFound);
}
似乎
intrandomidx=newrandom().Next(list.Count)
连续100次生成相同的数字,我想可能是因为种子是基于时间的

我怎样才能让它正常工作


谢谢:)

您不应该每次迭代都调用
new Random()
,因为它会导致重新设定种子并再次生成相同的数字序列。在应用程序开始时创建一个随机对象,并将其作为参数传递到函数中

public static class GenericIListExtensions
{
      public static T OneAtRandom<T>(this IList<T> list, Random random)
      {
         list.ThrowIfNull("list");
         if (list.Count == 0)
            throw new ArgumentException("OneAtRandom() cannot be called on 'list' with 0 elements");

         int randomIdx = random.Next(list.Count);
         return list[randomIdx];
      }
}
公共静态类GenericListExtensions
{
公共静态T OneAtRandom(此IList列表,随机)
{
列表。ThrowIfNull(“列表”);
如果(list.Count==0)
抛出新ArgumentException(“无法对包含0个元素的“列表”调用OneAtRandom());
int randomIdx=random.Next(list.Count);
返回列表[randomIdx];
}
}

这还具有使代码更易于测试的优点,因为您可以传入一个随机数,该随机数被设定为您选择的值,以便您的测试可以重复。

否;它100次生成相同的数字,因为您没有对生成器进行种子设定


将“new Random()”移动到构造函数或静态变量,并使用生成的对象。

您可以使用基于当前时间的种子来创建
Random
的实例。使用以下代码:

int randomInstancesToCreate = 4;
Random[] randomEngines = new Random[randomInstancesToCreate];
for (int ctr = 0; ctr < randomInstancesToCreate; ctr++)
{
   randomEngines[ctr] = new Random(unchecked((int) (DateTime.Now.Ticks >> ctr)));
}
int randomInstancesToCreate=4;
Random[]randomEngines=新随机[randomInstancesToCreate];
对于(int-ctr=0;ctr>ctr));
}

谢谢。我知道这是随机的():)