How to pass a Random point to function?

const std = @import("std");
const Random = std.Random;

pub fn main() !void {
    const rnd = std.rand.DefaultPrng.init(0).random();
    RndTest(&rnd);
}

pub fn RndTest(rnd: *const Random) void {
    std.debug.print("{d}", .{rnd.int(u16)});
}

Got below error:
src\main.zig:5:45: error: expected type ‘*Random.Xoshiro256’, found ‘*const Random.Xoshiro256’
const rnd = std.rand.DefaultPrng.init(0).random();

Thanks.

As the error message tells you, it expects a mutable reference of the generator, in this case Xoshiro256.
To fix this you need to store it in a sperate, mutable variable:

var prng = std.rand.DefaultPrng.init(0);
const rnd = prng.random();

By putting it all in one line you created a temporary variable, which is always constant.

3 Likes

Thanks.

And I learned one more thing : The temporary variable created in one line is always constant.

2 Likes