std::ffi::c::random (v0.2.2)
Raw C bindings for time and random numbers.
Latest
Overview
std::ffi::c::random exposes C functions such as time, clock, rand, and srand.
These call directly into the platform C library.
Source: thrustc/std/v0.2.2/ffi/c/random.thrust
Public Signatures
Exported declarations for this module snapshot.
fn time(timer: ptr[s64]) s64 @public @extern("time") @convention("C");
fn clock() s64 @public @extern("clock") @convention("C");
fn rand() s32 @public @extern("rand") @convention("C");
fn srand(seed: u32) void @public @extern("srand") @convention("C");
Behavior and Use
These functions call directly into the platform C library.
Prefer std::random for application code unless you need the raw C symbols.
Examples
import std::ffi::c::random as c;
// Seed C rand from the C clock. Use std::random for normal application code.
fn seedFromTime() void @public {
c::srand(c::time(nullptr) as u32);
}
import std::ffi::c::random as c;
// Keep modulo-based rand usage local so it can be replaced later if needed.
fn smallTicketNumber() s32 @public {
var value: s32 = c::rand();
if value < 0 {
value = 0 - value;
}
return value % 100;
}
import std::ffi::c::random as c;
// getentropy is available on Linux and Darwin in this raw C layer.
@if(isLinux() || isDarwin()) fn fillRandomBytes(buffer: ptr, length: usize) bool @public {
if length > 256 {
return false;
}
return c::getentropy(buffer, length) == 0;
}
@else fn fillRandomBytes(buffer: ptr, length: usize) bool @public {
return false;
}
Notes
- This is the raw C layer; std::random offers a higher-level interface.
- rand is not cryptographic and follows the C library behavior.