如何创建随机值的枚举? [英] How do you create an enumeration of random values?
本文介绍了如何创建随机值的枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获得随机值IEnumerable
?我很失望类随机没有实现IEnumerable<int>
.>
How can I get an IEnumerable
of random values? I'm disappointed the class Random doesn't implement IEnumerable<int>
.
推荐答案
编写一个扩展方法,该方法将yield return
个新值.
Write an extension method that will yield return
new values.
public static IEnumerable<int> GetRandomValues(this Random instance)
{
while(true)
{
yield return instance.Next();
}
}
这篇关于如何创建随机值的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文