Zig NEWS

loading...

How to use the random number generator in Zig

Govind
Updated on ・1 min read

A short post about using the random number generator in Zig.
For people coming from other languages, Random is an interface, that is implemented by other concrete structs to provide the functionality. For more information on how interfaces in Zig can be used , refer to this other excellent post on interfaces in Zig.

The Random interface is sort of embedded inside a concrete implementation.
The usage is as follows

const std = @import("std");
const RndGen = std.rand.DefaultPrng;

pub fn main() !void {
    var rnd = RndGen.init(0);
    var some_random_num = rnd.random().int(i32);
    std.debug.print("random number is {}", .{some_random_num});
}
Enter fullscreen mode Exit fullscreen mode

Previously, random was used by directly accessing a pointer to the embedded interface (rnd.random.init()). This has been updated in the current master, by an embedded function random() instead of a pointer to random.

DefaultPrng implements the Random interface and instead of calling rnd.init(i32) (which won't work), we invoke rnd.random().int.
The full list functions provided by the Random interface is listed in the std documentation

Discussion (2)

Collapse
kristoff profile image
Loris Cro

Thank you for the post. It's also worth pointing out that interfaces are about to change and in master branch Random has already changed. Before you would grab a pointer to the random field inside your specific implementation, while now random has become a function that you call to get the random interface.

var rand_impl = std.rand.DefaultPrng.init(42);
const num = rand_impl.random().int(i32);
Enter fullscreen mode Exit fullscreen mode
Collapse
gowind profile image
Govind Author

Yes, I read the source code. Will update the example.