#compile-time #constants #macro #rust

const-random

Provides compile time random number generation

18 releases

0.1.18 Mar 3, 2024
0.1.17 Oct 31, 2023
0.1.15 Oct 25, 2022
0.1.13 Dec 5, 2020
0.1.3 Mar 3, 2019

#197 in Rust patterns

Download history 99276/week @ 2023-12-23 162129/week @ 2023-12-30 201646/week @ 2024-01-06 218101/week @ 2024-01-13 212756/week @ 2024-01-20 220575/week @ 2024-01-27 234873/week @ 2024-02-03 236681/week @ 2024-02-10 229822/week @ 2024-02-17 266658/week @ 2024-02-24 268520/week @ 2024-03-02 296282/week @ 2024-03-09 293551/week @ 2024-03-16 279778/week @ 2024-03-23 291366/week @ 2024-03-30 240353/week @ 2024-04-06

1,151,896 downloads per month
Used in 952 crates (11 directly)

MIT/Apache

8KB

Random constants

This crate provides compile time random number generation. This allows you to insert random constants into your code that will be auto-generated at compile time.

A new value will be generated every time the file is rebuilt. This obviously makes the resulting binary or lib non-deterministic. (See below)

Example

use const_random::const_random  ;
const MY_RANDOM_NUMBER: u32 = const_random!(u32);

This works exactly as through you have called: OsRng.gen::<u32>() at compile time. So for details of the random number generation, see the rand crates documentation.

The following types are supported: u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize and [u8; N].

Deterministic builds

Sometimes it is an advantage for build systems to be deterministic. To support this const-random reads the environmental variable CONST_RANDOM_SEED. If this variable is set, it will be used as the seed for the random number generation. Setting the same seed on a build of the same code should result in identical output.

Dependencies

~135–310KB