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");
22const std = @import("std");
33const io = std.io;
44const fmt = std.fmt;
5const Rand = std.rand.Rand;
65const os = std.os;
76
87pub fn main() !void {
......@@ -14,15 +13,15 @@ pub fn main() !void {
1413
1514 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;
1817 os.getRandomBytes(seed_bytes[0..]) catch |err| {
1918 std.debug.warn("unable to seed random number generator: {}", err);
2019 return err;
2120 };
22 const seed = std.mem.readInt(seed_bytes, usize, builtin.Endian.Big);
23 var rand = Rand.init(seed);
21 const seed = std.mem.readInt(seed_bytes, u64, builtin.Endian.Big);
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
2726 while (true) {
2827 try stdout.print("\nGuess a number between 1 and 100: ");
src/parser.hpp-1
......@@ -16,7 +16,6 @@ ATTRIBUTE_PRINTF(2, 3)
1616void ast_token_error(Token *token, const char *format, ...);
1717
1818
19// This function is provided by generated code, generated by parsergen.cpp
2019AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color);
2120
2221void ast_print(AstNode *node, int indent);
std/rand/index.zig+25-25
......@@ -26,16 +26,16 @@ pub const DefaultPrng = Xoroshiro128;
2626// When you need cryptographically secure random numbers
2727pub const DefaultCsprng = Isaac64;
2828
29pub const Rand = struct {
30 fillFn: fn(r: &Rand, buf: []u8) void,
29pub const Random = struct {
30 fillFn: fn(r: &Random, buf: []u8) void,
3131
3232 /// 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 {
3434 r.fillFn(r, buf);
3535 }
3636
3737 /// 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 {
3939 var rand_bytes: [@sizeOf(T)]u8 = undefined;
4040 r.bytes(rand_bytes[0..]);
4141
......@@ -49,7 +49,7 @@ pub const Rand = struct {
4949
5050 /// Get a random unsigned integer with even distribution between `start`
5151 /// 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 {
5353 assert(start <= end);
5454 if (T.is_signed) {
5555 const uint = @IntType(false, T.bit_count);
......@@ -91,7 +91,7 @@ pub const Rand = struct {
9191 }
9292
9393 /// 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 {
9595 // Generate a uniform value between [1, 2) and scale down to [0, 1).
9696 // Note: The lowest mantissa bit is always set to 0 so we only use half the available range.
9797 switch (T) {
......@@ -110,18 +110,18 @@ pub const Rand = struct {
110110 }
111111
112112 /// 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 {
114114 // TODO(tiehuis): See https://www.doornik.com/research/ziggurat.pdf
115115 @compileError("floatNorm is unimplemented");
116116 }
117117
118118 /// 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 {
120120 @compileError("floatExp is unimplemented");
121121 }
122122
123123 /// 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 {
125125 if (buf.len < 2) {
126126 return;
127127 }
......@@ -178,14 +178,14 @@ test "splitmix64 sequence" {
178178pub const Pcg = struct {
179179 const default_multiplier = 6364136223846793005;
180180
181 random: Rand,
181 random: Random,
182182
183183 s: u64,
184184 i: u64,
185185
186186 pub fn init(init_s: u64) Pcg {
187187 var pcg = Pcg {
188 .random = Rand { .fillFn = fill },
188 .random = Random { .fillFn = fill },
189189 .s = undefined,
190190 .i = undefined,
191191 };
......@@ -218,7 +218,7 @@ pub const Pcg = struct {
218218 self.s = self.s *% default_multiplier +% self.i;
219219 }
220220
221 fn fill(r: &Rand, buf: []u8) void {
221 fn fill(r: &Random, buf: []u8) void {
222222 const self = @fieldParentPtr(Pcg, "random", r);
223223
224224 var i: usize = 0;
......@@ -269,13 +269,13 @@ test "pcg sequence" {
269269//
270270// PRNG
271271pub const Xoroshiro128 = struct {
272 random: Rand,
272 random: Random,
273273
274274 s: [2]u64,
275275
276276 pub fn init(init_s: u64) Xoroshiro128 {
277277 var x = Xoroshiro128 {
278 .random = Rand { .fillFn = fill },
278 .random = Random { .fillFn = fill },
279279 .s = undefined,
280280 };
281281
......@@ -328,7 +328,7 @@ pub const Xoroshiro128 = struct {
328328 self.s[1] = gen.next();
329329 }
330330
331 fn fill(r: &Rand, buf: []u8) void {
331 fn fill(r: &Random, buf: []u8) void {
332332 const self = @fieldParentPtr(Xoroshiro128, "random", r);
333333
334334 var i: usize = 0;
......@@ -397,7 +397,7 @@ test "xoroshiro sequence" {
397397// Follows the general idea of the implementation from here with a few shortcuts.
398398// https://doc.rust-lang.org/rand/src/rand/prng/isaac64.rs.html
399399pub const Isaac64 = struct {
400 random: Rand,
400 random: Random,
401401
402402 r: [256]u64,
403403 m: [256]u64,
......@@ -408,7 +408,7 @@ pub const Isaac64 = struct {
408408
409409 pub fn init(init_s: u64) Isaac64 {
410410 var isaac = Isaac64 {
411 .random = Rand { .fillFn = fill },
411 .random = Random { .fillFn = fill },
412412 .r = undefined,
413413 .m = undefined,
414414 .a = undefined,
......@@ -522,7 +522,7 @@ pub const Isaac64 = struct {
522522 self.i = self.r.len; // trigger refill on first value
523523 }
524524
525 fn fill(r: &Rand, buf: []u8) void {
525 fn fill(r: &Random, buf: []u8) void {
526526 const self = @fieldParentPtr(Isaac64, "random", r);
527527
528528 var i: usize = 0;
......@@ -577,8 +577,8 @@ test "isaac64 sequence" {
577577 }
578578}
579579
580// Actual Rand helper function tests, pcg engine is assumed correct.
581test "Rand float" {
580// Actual Random helper function tests, pcg engine is assumed correct.
581test "Random float" {
582582 var prng = DefaultPrng.init(0);
583583
584584 var i: usize = 0;
......@@ -593,18 +593,18 @@ test "Rand float" {
593593 }
594594}
595595
596test "Rand scalar" {
596test "Random scalar" {
597597 var prng = DefaultPrng.init(0);
598598 const s = prng .random.scalar(u64);
599599}
600600
601test "Rand bytes" {
601test "Random bytes" {
602602 var prng = DefaultPrng.init(0);
603603 var buf: [2048]u8 = undefined;
604604 prng.random.bytes(buf[0..]);
605605}
606606
607test "Rand shuffle" {
607test "Random shuffle" {
608608 var prng = DefaultPrng.init(0);
609609
610610 var seq = []const u8 { 0, 1, 2, 3, 4 };
......@@ -629,14 +629,14 @@ fn sumArray(s: []const u8) u32 {
629629 return r;
630630}
631631
632test "Rand range" {
632test "Random range" {
633633 var prng = DefaultPrng.init(0);
634634 testRange(&prng.random, -4, 3);
635635 testRange(&prng.random, -4, -1);
636636 testRange(&prng.random, 10, 14);
637637}
638638
639fn testRange(r: &Rand, start: i32, end: i32) void {
639fn testRange(r: &Random, start: i32, end: i32) void {
640640 const count = usize(end - start);
641641 var values_buffer = []bool{false} ** 20;
642642 const values = values_buffer[0..count];
std/sort.zig+1-1
......@@ -1091,7 +1091,7 @@ test "sort fuzz testing" {
10911091
10921092var fixed_buffer_mem: [100 * 1024]u8 = undefined;
10931093
1094fn fuzzTest(rng: &std.rand.Rand) void {
1094fn fuzzTest(rng: &std.rand.Random) void {
10951095 const array_size = rng.range(usize, 0, 1000);
10961096 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
10971097 var array = fixed_allocator.allocator.alloc(IdAndValue, array_size) catch unreachable;