-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Fisher-Yates/Knuth shuffling algorithm
- Loading branch information
1 parent
8d37813
commit d9302ef
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const std = @import("std"); | ||
const rand = std.rand; | ||
|
||
const Allocator = std.mem.Allocator; | ||
const swap = std.mem.swap; | ||
|
||
/// Shuffle slice in place with Fisher-Yates/Knuth. | ||
pub fn shuffle_in_place(comptime T: type, slice: []T, seed: u64) void { | ||
var prng = rand.DefaultPrng.init(seed); | ||
const random = prng.random(); | ||
|
||
var i: usize = undefined; | ||
if (@subWithOverflow(usize, slice.len, 1, &i)) { | ||
// Empty slice. Nothing to shuffle. | ||
i = 0; | ||
} | ||
|
||
// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm | ||
while (i > 0) { | ||
const j = random.uintAtMost(usize, i); | ||
swap(T, &slice[i], &slice[j]); | ||
|
||
i -= 1; | ||
} | ||
} | ||
|
||
pub fn shuffle_copy( | ||
comptime T: type, | ||
allocator: Allocator, | ||
slice: []T, | ||
seed: u64, | ||
) ![]T { | ||
// Copy slice to new memory to avoid mutating the original | ||
var shuffled = try allocator.alloc(T, slice.len); | ||
std.mem.copy(T, shuffled, slice); | ||
|
||
shuffle_in_place(T, shuffled, seed); | ||
|
||
return shuffled; | ||
} |