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});
}
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
Oldest comments (7)
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 therandom
field inside your specific implementation, while nowrandom
has become a function that you call to get the random interface.Yes, I read the source code. Will update the example.
Is there a way to cap the maximum? Like get a rnd between 0 and 32?
You can use the builtin
@mod
I keep forgetting if I should be holding on to a
std.rand.DefaultPrng
orstd.rand.Random
.The correct answer is to never hold on to
std.rand.Random
. Always access it via.random()
or else the internal seed won't be incremented and you will always get the same (ie. non-random) valuesWhat if you want to use a random seed?
The "tradition" I am aware of is to get the "now" timestamp.