C# 将列表随机化<;T>;
在C#中随机化泛型列表顺序的最佳方法是什么?我想给列表中的75个数字分配一个随机顺序,以便为彩票类型的应用程序抽取它们。如果您有一个固定数字(75),您可以创建一个包含75个元素的数组,然后枚举列表,将元素移动到数组中的随机位置。您可以使用生成列表编号到数组索引的映射。如果您有一个固定编号(75),您可以创建一个包含75个元素的数组,然后枚举列表,将元素移动到数组中的随机位置。您可以使用。生成列表编号到数组索引的映射,我通常使用:C# 将列表随机化<;T>;,c#,generic-list,C#,Generic List,在C#中随机化泛型列表顺序的最佳方法是什么?我想给列表中的75个数字分配一个随机顺序,以便为彩票类型的应用程序抽取它们。如果您有一个固定数字(75),您可以创建一个包含75个元素的数组,然后枚举列表,将元素移动到数组中的随机位置。您可以使用生成列表编号到数组索引的映射。如果您有一个固定编号(75),您可以创建一个包含75个元素的数组,然后枚举列表,将元素移动到数组中的随机位置。您可以使用。生成列表编号到数组索引的映射,我通常使用: var list = new List<T> ();
var list = new List<T> ();
fillList (list);
var randomizedList = new List<T> ();
var rnd = new Random ();
while (list.Count != 0)
{
var index = rnd.Next (0, list.Count);
randomizedList.Add (list [index]);
list.RemoveAt (index);
}
var list=newlist();
填充列表(列表);
var randomizedList=新列表();
var rnd=新的随机变量();
while(list.Count!=0)
{
var index=rnd.Next(0,list.Count);
添加(列表[索引]);
list.RemoveAt(索引);
}
我通常使用:
var list = new List<T> ();
fillList (list);
var randomizedList = new List<T> ();
var rnd = new Random ();
while (list.Count != 0)
{
var index = rnd.Next (0, list.Count);
randomizedList.Add (list [index]);
list.RemoveAt (index);
}
var list=newlist();
填充列表(列表);
var randomizedList=新列表();
var rnd=新的随机变量();
while(list.Count!=0)
{
var index=rnd.Next(0,list.Count);
添加(列表[索引]);
list.RemoveAt(索引);
}
解决此类问题的一个非常简单的方法是在列表中使用大量随机元素交换
在伪代码中,如下所示:
do
r1 = randomPositionInList()
r2 = randomPositionInList()
swap elements at index r1 and index r2
for a certain number of times
解决这类问题的一个非常简单的方法是在列表中使用一些随机元素交换 在伪代码中,如下所示:
do
r1 = randomPositionInList()
r2 = randomPositionInList()
swap elements at index r1 and index r2
for a certain number of times
公共静态列表随机化(列表)
{
List randomizedList=新列表();
随机rnd=新随机();
而(list.Count>0)
{
int index=rnd.Next(0,list.Count);//从主列表中选择一个随机项
randomizedList.Add(list[index]);//将其放在随机化列表的末尾
列表。删除(索引);
}
返回随机列表;
}
公共静态列表随机化(列表)
{
List randomizedList=新列表();
随机rnd=新随机();
而(list.Count>0)
{
int index=rnd.Next(0,list.Count);//从主列表中选择一个随机项
randomizedList.Add(list[index]);//将其放在随机化列表的末尾
列表。删除(索引);
}
返回随机列表;
}
使用扩展方法对任何(I)列表进行无序排列
,扩展方法基于:
private static Random rng=new Random();
公共静态无效洗牌(此IList列表)
{
int n=list.Count;
而(n>1){
n--;
int k=下一个(n+1);
T值=列表[k];
列表[k]=列表[n];
列表[n]=值;
}
}
用法:
List<Product> products = GetProducts();
products.Shuffle();
list.Sort(new RandomIntComparer());
List products=GetProducts();
products.Shuffle();
上面的代码使用了备受批评的System.Random方法来选择掉期候选者。它很快,但不像它应该的那样随机。如果在随机播放中需要更好的随机性,请使用System.Security.Cryptography中的随机数生成器,如下所示:
using System.Security.Cryptography;
...
public static void Shuffle<T>(this IList<T> list)
{
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
int n = list.Count;
while (n > 1)
{
byte[] box = new byte[1];
do provider.GetBytes(box);
while (!(box[0] < n * (Byte.MaxValue / n)));
int k = (box[0] % n);
n--;
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
使用System.Security.Cryptography;
...
公共静态无效洗牌(此IList列表)
{
RNGCryptoServiceProvider=新的RNGCryptoServiceProvider();
int n=list.Count;
而(n>1)
{
字节[]框=新字节[1];
do provider.GetBytes(框);
而(!(框[0]
可进行简单比较(回程机器)
编辑:自从几年前写下这个答案以来,很多人评论或写信给我,指出我比较中的一大愚蠢缺陷。他们当然是对的。这个系统并没有什么问题。若按预期的方式使用,那个么它是随机的。在上面的第一个示例中,我在Shuffle方法中实例化了rng变量,如果要重复调用该方法,这会带来麻烦。下面是一个固定的完整示例,它基于今天从@weston收到的一条非常有用的评论
Program.cs:
using System;
using System.Collections.Generic;
using System.Threading;
namespace SimpleLottery
{
class Program
{
private static void Main(string[] args)
{
var numbers = new List<int>(Enumerable.Range(1, 75));
numbers.Shuffle();
Console.WriteLine("The winning numbers are: {0}", string.Join(", ", numbers.GetRange(0, 5)));
}
}
public static class ThreadSafeRandom
{
[ThreadStatic] private static Random Local;
public static Random ThisThreadsRandom
{
get { return Local ?? (Local = new Random(unchecked(Environment.TickCount * 31 + Thread.CurrentThread.ManagedThreadId))); }
}
}
static class MyExtensions
{
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = ThreadSafeRandom.ThisThreadsRandom.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统线程;
名称空间简单模式
{
班级计划
{
私有静态void Main(字符串[]args)
{
var编号=新列表(可枚举范围(1,75));
numbers.Shuffle();
WriteLine(“中奖号码是:{0}”,string.Join(“,”,numbers.GetRange(0,5));
}
}
公共静态类ThreadSafeRandom
{
[ThreadStatic]私有静态随机本地;
公共静态随机线程随机
{
获取{return Local??(Local=new Random(未选中(Environment.TickCount*31+Thread.CurrentThread.ManagedThreadId));}
}
}
静态类MyExtensions
{
公共静态无效洗牌(此IList列表)
{
int n=list.Count;
而(n>1)
{
n--;
int k=ThreadSafeRandom.ThisThreadsRandom.Next(n+1);
T值=列表[k];
列表[k]=列表[n];
列表[n]=值;
}
}
}
}
使用扩展方法对任何(I)列表进行无序排列
,扩展方法基于:
private static Random rng=new Random();
公共静态无效洗牌(此IList列表)
{
int n=list.Count;
而(n>1){
n--;
int k=下一个(n+1);
T值=列表[k];
列表[k]=列表[n];
列表[n]=值;
}
}
用法:
List<Product> products = GetProducts();
products.Shuffle();
list.Sort(new RandomIntComparer());
List products=GetProducts();
products.Shuffle();
上面的代码使用了备受批评的System.Random方法来选择掉期候选者。它很快,但不像它应该的那样随机。如果在随机播放中需要更好的随机性,请使用System.Security.Cryptography中的随机数生成器,如下所示:
using System.Security.Cryptography;
...
public static void Shuffle<T>(this IList<T> list)
{
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
int n = list.Count;
while (n > 1)
{
byte[] box = new byte[1];
do provider.GetBytes(box);
while (!(box[0] < n * (Byte.MaxValue / n)));
int k = (box[0] % n);
n--;
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
u
public static IEnumerable<T> Shuffle<T>(this IList<T> list)
{
var choices = Enumerable.Range(0, list.Count).ToList();
var rng = new Random();
for(int n = choices.Count; n > 1; n--)
{
int k = rng.Next(n);
yield return list[choices[k]];
choices.RemoveAt(k);
}
yield return list[choices[0]];
}
public byte[] Shuffle(byte[] array, int start, int count)
{
int n = array.Length - start;
byte[] shuffled = new byte[count];
for(int i = 0; i < count; i++, start++)
{
int k = UniformRandomGenerator.Next(n--) + start;
shuffled[i] = array[k];
array[k] = array[start];
array[start] = shuffled[i];
}
return shuffled;
}
public static class EnumerableExtension
{
private static Random globalRng = new Random();
[ThreadStatic]
private static Random _rng;
private static Random rng
{
get
{
if (_rng == null)
{
int seed;
lock (globalRng)
{
seed = globalRng.Next();
}
_rng = new Random(seed);
}
return _rng;
}
}
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> items)
{
return items.OrderBy (i => rng.Next());
}
}
List<int> xList = new List<int>() { 1, 2, 3, 4, 5 };
List<int> deck = new List<int>();
foreach (int xInt in xList)
deck.Insert(random.Next(0, deck.Count + 1), xInt);
public static void Shuffle<T>(this IList<T> list, Random rnd)
{
for(var i=list.Count; i > 0; i--)
list.Swap(0, rnd.Next(0, i));
}
public static void Swap<T>(this IList<T> list, int i, int j)
{
var temp = list[i];
list[i] = list[j];
list[j] = temp;
}
public Deck(IEnumerable<Card> initialCards)
{
cards = new List<Card>(initialCards);
public void Shuffle()
}
{
List<Card> NewCards = new List<Card>();
while (cards.Count > 0)
{
int CardToMove = random.Next(cards.Count);
NewCards.Add(cards[CardToMove]);
cards.RemoveAt(CardToMove);
}
cards = NewCards;
}
public IEnumerable<string> GetCardNames()
{
string[] CardNames = new string[cards.Count];
for (int i = 0; i < cards.Count; i++)
CardNames[i] = cards[i].Name;
return CardNames;
}
Deck deck1;
Deck deck2;
Random random = new Random();
public Form1()
{
InitializeComponent();
ResetDeck(1);
ResetDeck(2);
RedrawDeck(1);
RedrawDeck(2);
}
private void ResetDeck(int deckNumber)
{
if (deckNumber == 1)
{
int numberOfCards = random.Next(1, 11);
deck1 = new Deck(new Card[] { });
for (int i = 0; i < numberOfCards; i++)
deck1.Add(new Card((Suits)random.Next(4),(Values)random.Next(1, 14)));
deck1.Sort();
}
else
deck2 = new Deck();
}
private void reset1_Click(object sender, EventArgs e) {
ResetDeck(1);
RedrawDeck(1);
}
private void shuffle1_Click(object sender, EventArgs e)
{
deck1.Shuffle();
RedrawDeck(1);
}
private void moveToDeck1_Click(object sender, EventArgs e)
{
if (listBox2.SelectedIndex >= 0)
if (deck2.Count > 0) {
deck1.Add(deck2.Deal(listBox2.SelectedIndex));
}
RedrawDeck(1);
RedrawDeck(2);
}
public static class IEnumerableExtensions
{
public static IEnumerable<t> Randomize<t>(this IEnumerable<t> target)
{
Random r = new Random();
return target.OrderBy(x=>(r.Next()));
}
}
// use this on any collection that implements IEnumerable!
// List, Array, HashSet, Collection, etc
List<string> myList = new List<string> { "hello", "random", "world", "foo", "bar", "bat", "baz" };
foreach (string s in myList.Randomize())
{
Console.WriteLine(s);
}
Items = Items.OrderBy(o => Guid.NewGuid().ToString()).ToList();
public static IList<T> NextList<T>(this Random r, IEnumerable<T> source)
{
var list = new List<T>();
foreach (var item in source)
{
var i = r.Next(list.Count + 1);
if (i == list.Count)
{
list.Add(item);
}
else
{
var temp = list[i];
list[i] = item;
list.Add(temp);
}
}
return list;
}
var bytes = new byte[8];
_secureRng.GetBytes(bytes);
var v = BitConverter.ToUInt64(bytes, 0);
return (double)v / ((double)ulong.MaxValue + 1);
return list[(int)(x * list.Count)];
private static Random rng = new Random();
/// <summary>
/// Returns a new list where the elements are randomly shuffled.
/// Based on the Fisher-Yates shuffle, which has O(n) complexity.
/// </summary>
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list) {
var source = list.ToList();
int n = source.Count;
var shuffled = new List<T>(n);
shuffled.AddRange(source);
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = shuffled[k];
shuffled[k] = shuffled[n];
shuffled[n] = value;
}
return shuffled;
}
var result = items.Select(x => new { value = x, order = rnd.Next() })
.OrderBy(x => x.order).Select(x => x.value).ToList()
List<T> OriginalList = new List<T>();
List<T> TempList = new List<T>();
Random random = new Random();
int length = OriginalList.Count;
int TempIndex = 0;
while (length > 0) {
TempIndex = random.Next(0, length); // get random value between 0 and original length
TempList.Add(OriginalList[TempIndex]); // add to temp list
OriginalList.RemoveAt(TempIndex); // remove from original list
length = OriginalList.Count; // get new list <T> length.
}
OriginalList = new List<T>();
OriginalList = TempList; // copy all items from temp list to original list.
collection.TakeRandom(5).SequenceEqual(collection.Shuffle().Take(5)); // true
public static IList<T> TakeRandom<T>(this IEnumerable<T> collection, int count, Random random) => shuffle(collection, count, random);
public static IList<T> Shuffle<T>(this IEnumerable<T> collection, Random random) => shuffle(collection, null, random);
private static IList<T> shuffle<T>(IEnumerable<T> collection, int? take, Random random)
{
var a = collection.ToArray();
var n = a.Length;
if (take <= 0 || take > n) throw new ArgumentException("Invalid number of elements to return.");
var end = take ?? n;
for (int i = 0; i < end; i++)
{
var j = random.Next(i, n);
(a[i], a[j]) = (a[j], a[i]);
}
if (take.HasValue) return new ArraySegment<T>(a, 0, take.Value);
return a;
}
public static void Shuffle<T>(this IList<T> list, Random rnd)
{
for (var i = list.Count-1; i > 0; i--)
{
var randomIndex = rnd.Next(i + 1); //maxValue (i + 1) is EXCLUSIVE
list.Swap(i, randomIndex);
}
}
public static void Swap<T>(this IList<T> list, int indexA, int indexB)
{
var temp = list[indexA];
list[indexA] = list[indexB];
list[indexB] = temp;
}
public class RandomIntComparer : IComparer<int>
{
private readonly Random _random = new Random();
public int Compare(int x, int y)
{
return _random.Next(-1, 2);
}
}
list.Sort(new RandomIntComparer());
using MoreLinq;
...
var randomized = list.Shuffle();
private List<GameObject> ShuffleList(List<GameObject> ActualList) {
List<GameObject> newList = ActualList;
List<GameObject> outList = new List<GameObject>();
int count = newList.Count;
while (newList.Count > 0) {
int rando = Random.Range(0, newList.Count);
outList.Add(newList[rando]);
newList.RemoveAt(rando);
}
return (outList);
}
List<GameObject> GetShuffle = ShuffleList(ActualList);