Skip to content

M_Cyjb_ArrayUtil_Suffle__1_2

CYJB edited this page Mar 20, 2024 · 13 revisions

ArrayUtil.Suffle(T) 方法 (T[])

将数组进行随机排序。

Namespace: Cyjb
Assembly: Cyjb (in Cyjb.dll) Version: 1.0.23+7750dd8e971297c5fa962a3bee37fb78f72793f6

语法

C#

public static T[] Suffle<T>(
	this T[] array
)

参数

 

array
Type: T[]
要进行随机排序的数组。

类型参数

 

T
数组中元素的类型。

返回值

Type: T[]
已完成随机排序的数组。

备注

在 Visual Basic 和 C# 中,这个方法可以当成为类型的实例方法来调用。在采用实例方法语法调用这个方法时,请省略第一个参数。请参考 扩展方法 (Visual Basic)扩展方法 (C# 编程指南) 获取更多信息。

Exceptions

 

异常 条件
ArgumentNullException arraynull

Remarks

应保证每个元素出现在每个位置的概率基本相同。 采用下面的代码进行测试:

int size = 10;
            int[] arr = new int[size];
            int[,] cnt = new int[size, size];
            for (int i = 0; i < 200; i++)
            {
                arr.Fill(n => n).Suffle();
                for (int j = 0; j < size; j++) cnt[j, arr[j]]++;
            }
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                    Console.Write("{0} ", cnt[i, j]);
                Console.WriteLine();
            }

参见

Reference

ArrayUtil 类
Suffle 重载
Cyjb 命名空间

Clone this wiki locally