authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-29 12:33:29-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-03-29 12:33:29-04:00
logccadcbc715c084f78dc093811d74f93b80536a0a
tree91cd02544b3e2450ad97cfe1ad2fb406ea662ead
parent0fd0f6fd1f4b9e02cc33eac304b9f6db242cbb67

fix examples and rename std.rand.Rand to std.rand.Random


4 files changed, 30 insertions(+), 32 deletions(-)

example/guess_number/main.zig+4-5
...@@ -2,7 +2,6 @@ const builtin = @import("builtin");...@@ -2,7 +2,6 @@ const builtin = @import("builtin");
2const std = @import("std");2const std = @import("std");
3const io = std.io;3const io = std.io;
4const fmt = std.fmt;4const fmt = std.fmt;
5const Rand = std.rand.Rand;
6const os = std.os;5const os = std.os;
76
8pub fn main() !void {7pub fn main() !void {
...@@ -14,15 +13,15 @@ pub fn main() !void {...@@ -14,15 +13,15 @@ pub fn main() !void {
1413
15 try stdout.print("Welcome to the Guess Number Game in Zig.\n");14 try stdout.print("Welcome to the Guess Number Game in Zig.\n");
1615
17 var seed_bytes: [@sizeOf(usize)]u8 = undefined;16 var seed_bytes: [@sizeOf(u64)]u8 = undefined;
18 os.getRandomBytes(seed_bytes[0..]) catch |err| {17 os.getRandomBytes(seed_bytes[0..]) catch |err| {
19 std.debug.warn("unable to seed random number generator: {}", err);18 std.debug.warn("unable to seed random number generator: {}", err);
20 return err;19 return err;
21 };20 };
22 const seed = std.mem.readInt(seed_bytes, usize, builtin.Endian.Big);21 const seed = std.mem.readInt(seed_bytes, u64, builtin.Endian.Big);
23 var rand = Rand.init(seed);22 var prng = std.rand.DefaultPrng.init(seed);
2423
25 const answer = rand.range(u8, 0, 100) + 1;24 const answer = prng.random.range(u8, 0, 100) + 1;
2625
27 while (true) {26 while (true) {
28 try stdout.print("\nGuess a number between 1 and 100: ");27 try stdout.print("\nGuess a number between 1 and 100: ");
src/parser.hpp-1
...@@ -16,7 +16,6 @@ ATTRIBUTE_PRINTF(2, 3)...@@ -16,7 +16,6 @@ ATTRIBUTE_PRINTF(2, 3)
16void ast_token_error(Token *token, const char *format, ...);16void ast_token_error(Token *token, const char *format, ...);
1717
1818
19// This function is provided by generated code, generated by parsergen.cpp
20AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color);19AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color);
2120
22void ast_print(AstNode *node, int indent);21void ast_print(AstNode *node, int indent);
std/rand/index.zig+25-25
...@@ -26,16 +26,16 @@ pub const DefaultPrng = Xoroshiro128;...@@ -26,16 +26,16 @@ pub const DefaultPrng = Xoroshiro128;
26// When you need cryptographically secure random numbers26// When you need cryptographically secure random numbers
27pub const DefaultCsprng = Isaac64;27pub const DefaultCsprng = Isaac64;
2828
29pub const Rand = struct {29pub const Random = struct {
30 fillFn: fn(r: &Rand, buf: []u8) void,30 fillFn: fn(r: &Random, buf: []u8) void,
3131
32 /// Read random bytes into the specified buffer until fill.32 /// Read random bytes into the specified buffer until fill.
33 pub fn bytes(r: &Rand, buf: []u8) void {33 pub fn bytes(r: &Random, buf: []u8) void {
34 r.fillFn(r, buf);34 r.fillFn(r, buf);
35 }35 }
3636
37 /// Return a random integer/boolean type.37 /// Return a random integer/boolean type.
38 pub fn scalar(r: &Rand, comptime T: type) T {38 pub fn scalar(r: &Random, comptime T: type) T {
39 var rand_bytes: [@sizeOf(T)]u8 = undefined;39 var rand_bytes: [@sizeOf(T)]u8 = undefined;
40 r.bytes(rand_bytes[0..]);40 r.bytes(rand_bytes[0..]);
4141
...@@ -49,7 +49,7 @@ pub const Rand = struct {...@@ -49,7 +49,7 @@ pub const Rand = struct {
4949
50 /// Get a random unsigned integer with even distribution between `start`50 /// Get a random unsigned integer with even distribution between `start`
51 /// inclusive and `end` exclusive.51 /// inclusive and `end` exclusive.
52 pub fn range(r: &Rand, comptime T: type, start: T, end: T) T {52 pub fn range(r: &Random, comptime T: type, start: T, end: T) T {
53 assert(start <= end);53 assert(start <= end);
54 if (T.is_signed) {54 if (T.is_signed) {
55 const uint = @IntType(false, T.bit_count);55 const uint = @IntType(false, T.bit_count);
...@@ -91,7 +91,7 @@ pub const Rand = struct {...@@ -91,7 +91,7 @@ pub const Rand = struct {
91 }91 }
9292
93 /// Return a floating point value evenly distributed in the range [0, 1).93 /// Return a floating point value evenly distributed in the range [0, 1).
94 pub fn float(r: &Rand, comptime T: type) T {94 pub fn float(r: &Random, comptime T: type) T {
95 // Generate a uniform value between [1, 2) and scale down to [0, 1).95 // Generate a uniform value between [1, 2) and scale down to [0, 1).
96 // Note: The lowest mantissa bit is always set to 0 so we only use half the available range.96 // Note: The lowest mantissa bit is always set to 0 so we only use half the available range.
97 switch (T) {97 switch (T) {
...@@ -110,18 +110,18 @@ pub const Rand = struct {...@@ -110,18 +110,18 @@ pub const Rand = struct {
110 }110 }
111111
112 /// Return a floating point value normally distributed in the range [0, 1].112 /// Return a floating point value normally distributed in the range [0, 1].
113 pub fn floatNorm(r: &Rand, comptime T: type) T {113 pub fn floatNorm(r: &Random, comptime T: type) T {
114 // TODO(tiehuis): See https://www.doornik.com/research/ziggurat.pdf114 // TODO(tiehuis): See https://www.doornik.com/research/ziggurat.pdf
115 @compileError("floatNorm is unimplemented");115 @compileError("floatNorm is unimplemented");
116 }116 }
117117
118 /// Return a exponentially distributed float between (0, @maxValue(f64))118 /// Return a exponentially distributed float between (0, @maxValue(f64))
119 pub fn floatExp(r: &Rand, comptime T: type) T {119 pub fn floatExp(r: &Random, comptime T: type) T {
120 @compileError("floatExp is unimplemented");120 @compileError("floatExp is unimplemented");
121 }121 }
122122
123 /// Shuffle a slice into a random order.123 /// Shuffle a slice into a random order.
124 pub fn shuffle(r: &Rand, comptime T: type, buf: []T) void {124 pub fn shuffle(r: &Random, comptime T: type, buf: []T) void {
125 if (buf.len < 2) {125 if (buf.len < 2) {
126 return;126 return;
127 }127 }
...@@ -178,14 +178,14 @@ test "splitmix64 sequence" {...@@ -178,14 +178,14 @@ test "splitmix64 sequence" {
178pub const Pcg = struct {178pub const Pcg = struct {
179 const default_multiplier = 6364136223846793005;179 const default_multiplier = 6364136223846793005;
180180
181 random: Rand,181 random: Random,
182182
183 s: u64,183 s: u64,
184 i: u64,184 i: u64,
185185
186 pub fn init(init_s: u64) Pcg {186 pub fn init(init_s: u64) Pcg {
187 var pcg = Pcg {187 var pcg = Pcg {
188 .random = Rand { .fillFn = fill },188 .random = Random { .fillFn = fill },
189 .s = undefined,189 .s = undefined,
190 .i = undefined,190 .i = undefined,
191 };191 };
...@@ -218,7 +218,7 @@ pub const Pcg = struct {...@@ -218,7 +218,7 @@ pub const Pcg = struct {
218 self.s = self.s *% default_multiplier +% self.i;218 self.s = self.s *% default_multiplier +% self.i;
219 }219 }
220220
221 fn fill(r: &Rand, buf: []u8) void {221 fn fill(r: &Random, buf: []u8) void {
222 const self = @fieldParentPtr(Pcg, "random", r);222 const self = @fieldParentPtr(Pcg, "random", r);
223223
224 var i: usize = 0;224 var i: usize = 0;
...@@ -269,13 +269,13 @@ test "pcg sequence" {...@@ -269,13 +269,13 @@ test "pcg sequence" {
269//269//
270// PRNG270// PRNG
271pub const Xoroshiro128 = struct {271pub const Xoroshiro128 = struct {
272 random: Rand,272 random: Random,
273273
274 s: [2]u64,274 s: [2]u64,
275275
276 pub fn init(init_s: u64) Xoroshiro128 {276 pub fn init(init_s: u64) Xoroshiro128 {
277 var x = Xoroshiro128 {277 var x = Xoroshiro128 {
278 .random = Rand { .fillFn = fill },278 .random = Random { .fillFn = fill },
279 .s = undefined,279 .s = undefined,
280 };280 };
281281
...@@ -328,7 +328,7 @@ pub const Xoroshiro128 = struct {...@@ -328,7 +328,7 @@ pub const Xoroshiro128 = struct {
328 self.s[1] = gen.next();328 self.s[1] = gen.next();
329 }329 }
330330
331 fn fill(r: &Rand, buf: []u8) void {331 fn fill(r: &Random, buf: []u8) void {
332 const self = @fieldParentPtr(Xoroshiro128, "random", r);332 const self = @fieldParentPtr(Xoroshiro128, "random", r);
333333
334 var i: usize = 0;334 var i: usize = 0;
...@@ -397,7 +397,7 @@ test "xoroshiro sequence" {...@@ -397,7 +397,7 @@ test "xoroshiro sequence" {
397// Follows the general idea of the implementation from here with a few shortcuts.397// Follows the general idea of the implementation from here with a few shortcuts.
398// https://doc.rust-lang.org/rand/src/rand/prng/isaac64.rs.html398// https://doc.rust-lang.org/rand/src/rand/prng/isaac64.rs.html
399pub const Isaac64 = struct {399pub const Isaac64 = struct {
400 random: Rand,400 random: Random,
401401
402 r: [256]u64,402 r: [256]u64,
403 m: [256]u64,403 m: [256]u64,
...@@ -408,7 +408,7 @@ pub const Isaac64 = struct {...@@ -408,7 +408,7 @@ pub const Isaac64 = struct {
408408
409 pub fn init(init_s: u64) Isaac64 {409 pub fn init(init_s: u64) Isaac64 {
410 var isaac = Isaac64 {410 var isaac = Isaac64 {
411 .random = Rand { .fillFn = fill },411 .random = Random { .fillFn = fill },
412 .r = undefined,412 .r = undefined,
413 .m = undefined,413 .m = undefined,
414 .a = undefined,414 .a = undefined,
...@@ -522,7 +522,7 @@ pub const Isaac64 = struct {...@@ -522,7 +522,7 @@ pub const Isaac64 = struct {
522 self.i = self.r.len; // trigger refill on first value522 self.i = self.r.len; // trigger refill on first value
523 }523 }
524524
525 fn fill(r: &Rand, buf: []u8) void {525 fn fill(r: &Random, buf: []u8) void {
526 const self = @fieldParentPtr(Isaac64, "random", r);526 const self = @fieldParentPtr(Isaac64, "random", r);
527527
528 var i: usize = 0;528 var i: usize = 0;
...@@ -577,8 +577,8 @@ test "isaac64 sequence" {...@@ -577,8 +577,8 @@ test "isaac64 sequence" {
577 }577 }
578}578}
579579
580// Actual Rand helper function tests, pcg engine is assumed correct.580// Actual Random helper function tests, pcg engine is assumed correct.
581test "Rand float" {581test "Random float" {
582 var prng = DefaultPrng.init(0);582 var prng = DefaultPrng.init(0);
583583
584 var i: usize = 0;584 var i: usize = 0;
...@@ -593,18 +593,18 @@ test "Rand float" {...@@ -593,18 +593,18 @@ test "Rand float" {
593 }593 }
594}594}
595595
596test "Rand scalar" {596test "Random scalar" {
597 var prng = DefaultPrng.init(0);597 var prng = DefaultPrng.init(0);
598 const s = prng .random.scalar(u64);598 const s = prng .random.scalar(u64);
599}599}
600600
601test "Rand bytes" {601test "Random bytes" {
602 var prng = DefaultPrng.init(0);602 var prng = DefaultPrng.init(0);
603 var buf: [2048]u8 = undefined;603 var buf: [2048]u8 = undefined;
604 prng.random.bytes(buf[0..]);604 prng.random.bytes(buf[0..]);
605}605}
606606
607test "Rand shuffle" {607test "Random shuffle" {
608 var prng = DefaultPrng.init(0);608 var prng = DefaultPrng.init(0);
609609
610 var seq = []const u8 { 0, 1, 2, 3, 4 };610 var seq = []const u8 { 0, 1, 2, 3, 4 };
...@@ -629,14 +629,14 @@ fn sumArray(s: []const u8) u32 {...@@ -629,14 +629,14 @@ fn sumArray(s: []const u8) u32 {
629 return r;629 return r;
630}630}
631631
632test "Rand range" {632test "Random range" {
633 var prng = DefaultPrng.init(0);633 var prng = DefaultPrng.init(0);
634 testRange(&prng.random, -4, 3);634 testRange(&prng.random, -4, 3);
635 testRange(&prng.random, -4, -1);635 testRange(&prng.random, -4, -1);
636 testRange(&prng.random, 10, 14);636 testRange(&prng.random, 10, 14);
637}637}
638638
639fn testRange(r: &Rand, start: i32, end: i32) void {639fn testRange(r: &Random, start: i32, end: i32) void {
640 const count = usize(end - start);640 const count = usize(end - start);
641 var values_buffer = []bool{false} ** 20;641 var values_buffer = []bool{false} ** 20;
642 const values = values_buffer[0..count];642 const values = values_buffer[0..count];
std/sort.zig+1-1
...@@ -1091,7 +1091,7 @@ test "sort fuzz testing" {...@@ -1091,7 +1091,7 @@ test "sort fuzz testing" {
10911091
1092var fixed_buffer_mem: [100 * 1024]u8 = undefined;1092var fixed_buffer_mem: [100 * 1024]u8 = undefined;
10931093
1094fn fuzzTest(rng: &std.rand.Rand) void {1094fn fuzzTest(rng: &std.rand.Random) void {
1095 const array_size = rng.range(usize, 0, 1000);1095 const array_size = rng.range(usize, 0, 1000);
1096 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);1096 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
1097 var array = fixed_allocator.allocator.alloc(IdAndValue, array_size) catch unreachable;1097 var array = fixed_allocator.allocator.alloc(IdAndValue, array_size) catch unreachable;