Skip to content

Commit b92c5af

Browse files
committed
Added shuffling feature.
1 parent e969dd0 commit b92c5af

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Assets/ExpMngr/Scripts/Block.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public Trial GetRelativeTrial(int relativeTrialNumber)
5656
return trials[relativeTrialNumber - 1];
5757
}
5858

59+
60+
61+
5962
}
6063

6164
}

Assets/ExpMngr/Scripts/ExperimentSession.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public class ExperimentSession : MonoBehaviour
108108
public Block currentBlock { get { return GetBlock(); } }
109109

110110
/// <summary>
111-
/// return trials for all blocks
111+
/// Returns a list of trials for all blocks. Modifying the order of this list will not affect trial order. Modify block.trials to change order within blocks.
112+
///
112113
/// </summary>
113114
public List<Trial> trials
114115
{

Assets/ExpMngr/Scripts/Extensions.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
static class Extensions
88
{
9+
10+
/// <summary>
11+
/// Random number generator with seed based on current time.
12+
/// </summary>
13+
/// <returns></returns>
14+
private static System.Random rng = new System.Random();
15+
916
public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
1017
{
1118
return listToClone.Select(item => (T)item.Clone()).ToList();
@@ -15,4 +22,33 @@ public static string GetSafeFilename(string filename)
1522
{
1623
return string.Join("", filename.Split(Path.GetInvalidFileNameChars()));
1724
}
25+
26+
/// <summary>
27+
/// Shuffles a list in-place with a given random number generator.
28+
/// </summary>
29+
/// <param name="list">List to shuffle</param>
30+
/// <param name="rng">Random number generator via which the shuffling occurs</param>
31+
public static void Shuffle<T>(this IList<T> list, System.Random rng)
32+
{
33+
34+
int n = list.Count;
35+
while (n > 1)
36+
{
37+
n--;
38+
int k = rng.Next(n + 1);
39+
T value = list[k];
40+
list[k] = list[n];
41+
list[n] = value;
42+
}
43+
}
44+
45+
/// <summary>
46+
/// Shuffles a list in-place with the current time based random number generator.
47+
/// </summary>
48+
/// <param name="list">List to shuffle</param>
49+
public static void Shuffle<T>(this IList<T> list)
50+
{
51+
list.Shuffle(rng);
52+
}
53+
1854
}

0 commit comments

Comments
 (0)