Skip to content

Commit

Permalink
publicize std.rand.ziggurat
Browse files Browse the repository at this point in the history
  • Loading branch information
Evin Yulo authored and andrewrk committed Jun 13, 2023
1 parent 21c258a commit 129afba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/std/rand.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const builtin = @import("builtin");
const assert = std.debug.assert;
const mem = std.mem;
const math = std.math;
const ziggurat = @import("rand/ziggurat.zig");
const maxInt = std.math.maxInt;

/// Fast unbiased random numbers.
Expand All @@ -29,6 +28,7 @@ pub const Xoroshiro128 = @import("rand/Xoroshiro128.zig");
pub const Xoshiro256 = @import("rand/Xoshiro256.zig");
pub const Sfc64 = @import("rand/Sfc64.zig");
pub const RomuTrio = @import("rand/RomuTrio.zig");
pub const ziggurat = @import("rand/ziggurat.zig");

pub const Random = struct {
ptr: *anyopaque,
Expand Down
22 changes: 11 additions & 11 deletions lib/std/rand/ziggurat.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub const ZigTable = struct {
};

// zigNorInit
fn ZigTableGen(
pub fn ZigTableGen(
comptime is_symmetric: bool,
comptime r: f64,
comptime v: f64,
Expand Down Expand Up @@ -102,16 +102,16 @@ pub const NormDist = blk: {
break :blk ZigTableGen(true, norm_r, norm_v, norm_f, norm_f_inv, norm_zero_case);
};

const norm_r = 3.6541528853610088;
const norm_v = 0.00492867323399;
pub const norm_r = 3.6541528853610088;
pub const norm_v = 0.00492867323399;

fn norm_f(x: f64) f64 {
pub fn norm_f(x: f64) f64 {
return @exp(-x * x / 2.0);
}
fn norm_f_inv(y: f64) f64 {
pub fn norm_f_inv(y: f64) f64 {
return @sqrt(-2.0 * @log(y));
}
fn norm_zero_case(random: Random, u: f64) f64 {
pub fn norm_zero_case(random: Random, u: f64) f64 {
var x: f64 = 1;
var y: f64 = 0;

Expand Down Expand Up @@ -143,16 +143,16 @@ pub const ExpDist = blk: {
break :blk ZigTableGen(false, exp_r, exp_v, exp_f, exp_f_inv, exp_zero_case);
};

const exp_r = 7.69711747013104972;
const exp_v = 0.0039496598225815571993;
pub const exp_r = 7.69711747013104972;
pub const exp_v = 0.0039496598225815571993;

fn exp_f(x: f64) f64 {
pub fn exp_f(x: f64) f64 {
return @exp(-x);
}
fn exp_f_inv(y: f64) f64 {
pub fn exp_f_inv(y: f64) f64 {
return -@log(y);
}
fn exp_zero_case(random: Random, _: f64) f64 {
pub fn exp_zero_case(random: Random, _: f64) f64 {
return exp_r - @log(random.float(f64));
}

Expand Down

0 comments on commit 129afba

Please sign in to comment.