15 releases

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

#36 in Procedural macros

Download history 91935/week @ 2022-10-08 101604/week @ 2022-10-15 83218/week @ 2022-10-22 70013/week @ 2022-10-29 70951/week @ 2022-11-05 119923/week @ 2022-11-12 78793/week @ 2022-11-19 122437/week @ 2022-11-26 110568/week @ 2022-12-03 111828/week @ 2022-12-10 67616/week @ 2022-12-17 43290/week @ 2022-12-24 94850/week @ 2022-12-31 132160/week @ 2023-01-07 133589/week @ 2023-01-14 116733/week @ 2023-01-21

484,926 downloads per month
Used in 202 crates (7 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

~185KB