|
@@ -79,9 +79,9 @@ export class Utils
|
79
|
79
|
}
|
80
|
80
|
|
81
|
81
|
/**
|
82
|
|
- * Returns a range [0, 1, ..., n-1]
|
|
82
|
+ * Generate the range [0, 1, ..., n-1]
|
83
|
83
|
* @param n non-negative integer
|
84
|
|
- * @returns range from 0 to n-1, inclusive
|
|
84
|
+ * @returns range from 0 to n-1, inclusive, as a new array
|
85
|
85
|
*/
|
86
|
86
|
static range(n: number): number[]
|
87
|
87
|
{
|
|
@@ -92,6 +92,24 @@ export class Utils
|
92
|
92
|
}
|
93
|
93
|
|
94
|
94
|
/**
|
|
95
|
+ * Shuffle an array
|
|
96
|
+ * @param arr array to be shuffled in-place
|
|
97
|
+ * @returns shuffled arr
|
|
98
|
+ */
|
|
99
|
+ static shuffle<T>(arr: T[]): T[]
|
|
100
|
+ {
|
|
101
|
+ // Fisher-Yattes shuffle
|
|
102
|
+ for(let i = arr.length - 1; i >= 1; i--) {
|
|
103
|
+ const j = Math.floor(Math.random() * (i + 1)); // 0 <= j <= i
|
|
104
|
+ const tmp = arr[i];
|
|
105
|
+ arr[i] = arr[j];
|
|
106
|
+ arr[j] = tmp;
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ return arr;
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ /**
|
95
|
113
|
* Wait a few milliseconds
|
96
|
114
|
* @param milliseconds how long should we wait?
|
97
|
115
|
* @returns a promise that is resolved soon after the specified time
|