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

#170 in Procedural macros

Download history 1211435/week @ 2025-10-25 1203320/week @ 2025-11-01 1227297/week @ 2025-11-08 1280072/week @ 2025-11-15 1051616/week @ 2025-11-22 1137332/week @ 2025-11-29 1410346/week @ 2025-12-06 1385222/week @ 2025-12-13 810218/week @ 2025-12-20 743474/week @ 2025-12-27 1433271/week @ 2026-01-03 1618078/week @ 2026-01-10 1647281/week @ 2026-01-17 1645654/week @ 2026-01-24 1691446/week @ 2026-01-31 1731220/week @ 2026-02-07

6,979,814 downloads per month
Used in 3,493 crates (22 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

~250KB