Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/akhildevelops/cudaz/llms.txt

Use this file to discover all available pages before exploring further.

Rng provides GPU-accelerated random number generation backed by the cuRAND library. It wraps a curandGenerator_t handle and a Device, exposing a simple interface for seeding the generator and producing arrays of uniform random f32 values entirely on the GPU. Generation is performed by curandGenerateUniform, which fills a device-side buffer without any host involvement in the sampling loop.

Import

const CuRng = @import("cudaz").Rng;

Fields

FieldTypeDescription
rngcurandGenerator_tThe underlying cuRAND generator handle
deviceDeviceThe device associated with this generator

Functions

default

pub fn default() !@This()
Creates an Rng on GPU 0 with seed 0. Uses the CURAND_RNG_PSEUDO_DEFAULT generator algorithm (a high-quality pseudo-random generator chosen by cuRAND). Internally calls Device.default() to initialize the device. Returns: !@This() (an initialized Rng)
const rng = try CuRng.default();
const slice = try rng.genrandom(512);
defer slice.free();

init

pub fn init(device: Device, seed: ?u64) !@This()
Creates an Rng on a specific device with a given seed. Pass null for seed to use the default seed of 0. Uses CURAND_RNG_PSEUDO_DEFAULT for the generator type, same as default.
device
Device
required
An already-initialized Device on which the RNG will operate. The generator uses the current context of this device.
seed
?u64
The 64-bit seed for the pseudo-random sequence. Pass null to use 0. Different seeds produce independent random sequences; the same seed always produces the same sequence.
Returns: !@This() (an initialized Rng)
const device = try Cuda.Device.default();
defer device.deinit();

const rng = try CuRng.init(device, 42);

genrandom

pub fn genrandom(self: @This(), size: usize) !CudaSlice(f32)
Generates size uniform random f32 values in the half-open interval [0, 1) entirely on the GPU using curandGenerateUniform. Allocates a CudaSlice(f32) to hold the output; the caller owns the returned slice and must call slice.free() when done.
self
@This()
required
The initialized RNG to sample from. Each call advances the generator’s internal state.
size
usize
required
Number of random f32 values to generate. Must be even (cuRAND requirement for some generator types).
Returns: !CudaSlice(f32)

Example

const std = @import("std");
const Cuda = @import("cudaz");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();

    const device = try Cuda.Device.default();
    defer device.deinit();

    const rng = try Cuda.Rng.init(device, 12345);
    const slice = try rng.genrandom(1000);
    defer slice.free();

    var arr = try Cuda.Device.syncReclaim(f32, allocator, slice);
    defer arr.deinit();
    // arr.items contains 1000 uniform random f32 values in [0, 1)

    std.debug.print("first value: {d}\n", .{arr.items[0]});
}
The curand system library must be linked in your build.zig for Rng to work:
exe.root_module.linkSystemLibrary("curand", .{});
Without this, compilation will fail with an undefined symbol error for curandCreateGenerator.

Build docs developers (and LLMs) love