需要随机数的操作也接收同一个 std.Io 上下文。std.Io.randomSecure 使用上下文中的安全随机 源,并填充调用方提供的缓冲区。现有 Network 示例还演示了 TCP、UDP、DNS 和 HTTP 使用的 std.Io.net 接口。

//! Get random bytes through the Io context.

const std = @import("std");

pub fn main(init: std.process.Init) !void {
    var random_bytes: [8]u8 = undefined;
    try std.Io.randomSecure(init.io, &random_bytes);

    std.debug.print("secure random bytes: {x}\n", .{random_bytes});
}