Something went wrong. Try again.
A puzzle game of bent arrows sent off a board of dots, written in Zig.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! SplitMix64, and the few ways the generator draws from it.//!//! This is ours rather than `std.Random` because a level is a seed, and the//! same seed has to build the same board on every platform and after every//! upgrade of Zig. The standard library is free to change which generator//! `DefaultPrng` names and how it reduces a number into a range; neither//! may change here without changing every level in the game.
const Random = @This();
state: u64,
pub fn init(seed: u64) Random { return .{ .state = seed };}
pub fn next(r: *Random) u64 { r.state +%= 0x9e3779b97f4a7c15; var z = r.state; z = (z ^ (z >> 30)) *% 0xbf58476d1ce4e5b9; z = (z ^ (z >> 27)) *% 0x94d049bb133111eb; return z ^ (z >> 31);}
/// A number in `[0, n)`, by multiplying and keeping the high half. The bias/// is at most n / 2^64, which no board is large enough to notice.pub fn below(r: *Random, n: u64) u64 { return @intCast((@as(u128, r.next()) * n) >> 64);}
/// A number in `[low, high]`.pub fn between(r: *Random, low: u64, high: u64) u64 { return low + r.below(high - low + 1);}
/// True with a chance of `percent` in a hundred.pub fn chance(r: *Random, percent: u8) bool { return r.below(100) < percent;}
test "the sequence is SplitMix64's" { // The first outputs for seed 0, from the reference implementation. var r: Random = .init(0); const std = @import("std"); try std.testing.expectEqual(0xe220a8397b1dcdaf, r.next()); try std.testing.expectEqual(0x6e789e6aa1b965f4, r.next()); try std.testing.expectEqual(0x06c45d188009454f, r.next());}