-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFisherYatesShuffle.cs
54 lines (46 loc) · 1.78 KB
/
FisherYatesShuffle.cs
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
// Ishan Pranav's REBUS: FisherYatesShuffle.cs
// Copyright (c) 2021-2022 Ishan Pranav. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
namespace Rebus.Server
{
/// <summary>
/// Performs Durstenfeld's Fisher–Yates shuffle algorithm to randomize collections.
/// </summary>
/// <remarks>
/// The implementation of this class was inspired by and based on <see href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">this</see> Wikipedia article.
/// </remarks>
/// <seealso href="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">Fisher–Yates shuffle - Wikipedia</seealso>
public class FisherYatesShuffle
{
/// <summary>
/// Gets the pseudo-random number generator.
/// </summary>
/// <value>The pseudo-random number generator.</value>
public Random Random { get; }
/// <summary>
/// Initializes a new instance of the <see cref="FisherYatesShuffle"/> class.
/// </summary>
/// <param name="random">The pseudo-random number generator.</param>
public FisherYatesShuffle(Random random)
{
Random = random;
}
/// <summary>
/// Shuffles a collection using Durstenfeld's algorithm.
/// </summary>
/// <param name="values">The collection.</param>
public void Shuffle(IList<string> values)
{
// for i from n−1 downto 1 do
for (int i = values.Count - 1; i >= 1; i--)
{
// j ← random integer such that 0 ≤ j ≤ i
int j = Random.Next(i + 1);
// exchange a[j] and a[i]
(values[i], values[j]) = (values[j], values[i]);
}
}
}
}