Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tkaitchuck/constrandom
Macro to generate random constants in Rust https://xkcd.com/221/
https://github.com/tkaitchuck/constrandom
constants macro rust
Last synced: 6 days ago
JSON representation
Macro to generate random constants in Rust https://xkcd.com/221/
- Host: GitHub
- URL: https://github.com/tkaitchuck/constrandom
- Owner: tkaitchuck
- License: apache-2.0
- Created: 2019-03-02T23:49:42.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-03T23:31:19.000Z (10 months ago)
- Last Synced: 2024-12-13T14:44:06.535Z (21 days ago)
- Topics: constants, macro, rust
- Language: Rust
- Homepage:
- Size: 65.4 KB
- Stars: 74
- Watchers: 6
- Forks: 14
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# 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
```rust
use const_random::const_random ;
const MY_RANDOM_NUMBER: u32 = const_random!(u32);
```
This works exactly as through you have called: `OsRng.gen::()` 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.