#constants #macro #random #proc-macro #rust

macro const-random-macro

Provides the procedural macro used by const-random

16 releases

0.1.16 Oct 23, 2023
0.1.15 Oct 25, 2022
0.1.13 Dec 5, 2020
0.1.12 Nov 21, 2020
0.1.3 Mar 3, 2019

#1148 in Procedural macros

Download history 226961/week @ 2024-01-17 210387/week @ 2024-01-24 235226/week @ 2024-01-31 223268/week @ 2024-02-07 236574/week @ 2024-02-14 246918/week @ 2024-02-21 273635/week @ 2024-02-28 262791/week @ 2024-03-06 294604/week @ 2024-03-13 284447/week @ 2024-03-20 271983/week @ 2024-03-27 283687/week @ 2024-04-03 301378/week @ 2024-04-10 349383/week @ 2024-04-17 332864/week @ 2024-04-24 249311/week @ 2024-05-01

1,286,302 downloads per month
Used in 1,001 crates (via const-random)

MIT/Apache

7KB
138 lines

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

~130–310KB