forked from laochiangx/Common.Utility
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomHelper.cs
More file actions
83 lines (73 loc) · 2.38 KB
/
RandomHelper.cs
File metadata and controls
83 lines (73 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
namespace Common.Utility
{
/// <summary>
/// 使用Random类生成伪随机数
/// </summary>
public class RandomHelper
{
//随机数对象
private Random _random;
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
public RandomHelper()
{
//为随机数对象赋值
this._random = new Random();
}
#endregion
#region 生成一个指定范围的随机整数
/// <summary>
/// 生成一个指定范围的随机整数,该随机数范围包括最小值,但不包括最大值
/// </summary>
/// <param name="minNum">最小值</param>
/// <param name="maxNum">最大值</param>
public int GetRandomInt(int minNum, int maxNum)
{
return this._random.Next(minNum, maxNum);
}
#endregion
#region 生成一个0.0到1.0的随机小数
/// <summary>
/// 生成一个0.0到1.0的随机小数
/// </summary>
public double GetRandomDouble()
{
return this._random.NextDouble();
}
#endregion
#region 对一个数组进行随机排序
/// <summary>
/// 对一个数组进行随机排序
/// </summary>
/// <typeparam name="T">数组的类型</typeparam>
/// <param name="arr">需要随机排序的数组</param>
public void GetRandomArray<T>(T[] arr)
{
//对数组进行随机排序的算法:随机选择两个位置,将两个位置上的值交换
//交换的次数,这里使用数组的长度作为交换次数
int count = arr.Length;
//开始交换
for (int i = 0; i < count; i++)
{
//生成两个随机数位置
int targetIndex1 = GetRandomInt(0, arr.Length);
int targetIndex2 = GetRandomInt(0, arr.Length);
//定义临时变量
T temp;
//交换两个随机数位置的值
temp = arr[targetIndex1];
arr[targetIndex1] = arr[targetIndex2];
arr[targetIndex2] = temp;
}
}
internal static int GetRandomSeed()
{
throw new NotImplementedException();
}
#endregion
}
}