Skip to content

Commit

Permalink
Merge pull request #867 from zig-lang/rand-overhaul
Browse files Browse the repository at this point in the history
Rewrite Rand functions
  • Loading branch information
andrewrk committed Mar 29, 2018
2 parents 9df2a6a + ccadcbc commit b80398b
Show file tree
Hide file tree
Showing 9 changed files with 740 additions and 837 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -509,7 +509,7 @@ set(ZIG_STD_FILES
"os/windows/index.zig"
"os/windows/util.zig"
"os/zen.zig"
"rand.zig"
"rand/index.zig"
"sort.zig"
"special/bootstrap.zig"
"special/bootstrap_lib.zig"
Expand Down
9 changes: 4 additions & 5 deletions example/guess_number/main.zig
Expand Up @@ -2,7 +2,6 @@ const builtin = @import("builtin");
const std = @import("std");
const io = std.io;
const fmt = std.fmt;
const Rand = std.rand.Rand;
const os = std.os;

pub fn main() !void {
Expand All @@ -14,15 +13,15 @@ pub fn main() !void {

try stdout.print("Welcome to the Guess Number Game in Zig.\n");

var seed_bytes: [@sizeOf(usize)]u8 = undefined;
var seed_bytes: [@sizeOf(u64)]u8 = undefined;
os.getRandomBytes(seed_bytes[0..]) catch |err| {
std.debug.warn("unable to seed random number generator: {}", err);
return err;
};
const seed = std.mem.readInt(seed_bytes, usize, builtin.Endian.Big);
var rand = Rand.init(seed);
const seed = std.mem.readInt(seed_bytes, u64, builtin.Endian.Big);
var prng = std.rand.DefaultPrng.init(seed);

const answer = rand.range(u8, 0, 100) + 1;
const answer = prng.random.range(u8, 0, 100) + 1;

while (true) {
try stdout.print("\nGuess a number between 1 and 100: ");
Expand Down
1 change: 0 additions & 1 deletion src/parser.hpp
Expand Up @@ -16,7 +16,6 @@ ATTRIBUTE_PRINTF(2, 3)
void ast_token_error(Token *token, const char *format, ...);


// This function is provided by generated code, generated by parsergen.cpp
AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color);

void ast_print(AstNode *node, int indent);
Expand Down
4 changes: 2 additions & 2 deletions std/index.zig
Expand Up @@ -26,7 +26,7 @@ pub const math = @import("math/index.zig");
pub const mem = @import("mem.zig");
pub const net = @import("net.zig");
pub const os = @import("os/index.zig");
pub const rand = @import("rand.zig");
pub const rand = @import("rand/index.zig");
pub const sort = @import("sort.zig");
pub const unicode = @import("unicode.zig");
pub const zig = @import("zig/index.zig");
Expand Down Expand Up @@ -58,7 +58,7 @@ test "std" {
_ = @import("heap.zig");
_ = @import("net.zig");
_ = @import("os/index.zig");
_ = @import("rand.zig");
_ = @import("rand/index.zig");
_ = @import("sort.zig");
_ = @import("unicode.zig");
_ = @import("zig/index.zig");
Expand Down
6 changes: 3 additions & 3 deletions std/io_test.zig
@@ -1,16 +1,16 @@
const std = @import("index.zig");
const io = std.io;
const allocator = std.debug.global_allocator;
const Rand = std.rand.Rand;
const DefaultPrng = std.rand.DefaultPrng;
const assert = std.debug.assert;
const mem = std.mem;
const os = std.os;
const builtin = @import("builtin");

test "write a file, read it, then delete it" {
var data: [1024]u8 = undefined;
var rng = Rand.init(1234);
rng.fillBytes(data[0..]);
var prng = DefaultPrng.init(1234);
prng.random.bytes(data[0..]);
const tmp_file_name = "temp_test_file.txt";
{
var file = try os.File.openWrite(allocator, tmp_file_name);
Expand Down
240 changes: 0 additions & 240 deletions std/rand.zig

This file was deleted.

0 comments on commit b80398b

Please sign in to comment.