Zig Cookbook

介绍

生成随机数

使用 std.Random.IoSource 生成随机数,它提供了一个由系统 I/O 接口支持的加密安全随机数生成器。

const std = @import("std");
const print = std.debug.print;

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    var source: std.Random.IoSource = .{ .io = io };
    const rand = source.interface();

    print("Random u8: {}\n", .{rand.int(u8)});
    print("Random u8 less than 10: {}\n", .{rand.uintLessThan(u8, 10)});
    print("Random u16: {}\n", .{rand.int(u16)});
    print("Random u32: {}\n", .{rand.int(u32)});
    print("Random i32: {}\n", .{rand.int(i32)});
    print("Random float: {d}\n", .{rand.float(f64)});

    var i: usize = 0;
    while (i < 9) {
        print("Random enum: {}\n", .{rand.enumValue(enum { red, green, blue })});
        i += 1;
    }
}

上一示例:http.Server - std
下一示例:启动短期线程