Macro nekolib::rand_gen

source ·
macro_rules! rand_gen {
    ( @seed $seed:ident { mut $a:ident in $($r:tt)* } @rest ) => { ... };
    ( @seed $seed:ident { $a:ident in $($r:tt)* } @rest ) => { ... };
    ( @seed $seed:ident { $a:ident in $($r:tt)* } @rest ; $($rest:tt)* ) => { ... };
    ( @seed $seed:ident { mut $a:ident in $($r:tt)* } @rest ; $($rest:tt)* ) => { ... };
    ( @seed $seed:ident { $($cur:tt)* } @rest $tt:tt $($rest:tt)* ) => { ... };
    ( @seed $seed:ident {} @rest ) => { ... };
    ( $seed:ident: $ty:ty; $($rest:tt)* ) => { ... };
    ( $seed:ident = $s:expr; $($rest:tt)* ) => { ... };
}
Expand description

乱数生成マクロ。

Notes

作りかけなのでいろいろ足りない。

Examples

use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;

use nekolib::rand_gen;
use nekolib::utils::ascii::*;
use nekolib::utils::rand_gen_macro::*;

rand_gen! {
    rng: ChaCha20Rng;

    n in 1_usize..=10;
    a in [1_i64..=100; n];
    s in AsciiString(16) where {
        distribution = &[
            (ASCII_LOWERCASE, 10),
            (ASCII_UPPERCASE, 10),
            (ASCII_DIGIT, 6),
            (charset(b"~!@#$%+?|()^*_-=[]{};:,./"), 5),
        ],
    };
}

assert_eq!(a.len(), n);
assert!(a.iter().all(|ai| (1..=100).contains(ai)));
assert_eq!(s.len(), 16);
// Possible value of `s`: `"3e)xIjos2^M/XI1T"`, `"X52dhjDk%i6)p1F9"`

以下のような出力が得られるため、これを用いて再現することができる。

To reproduce:

```
rand_gen! {
    rng = ChaCha20Rng::from_seed([250, 120, 31, 164, 15, 176, 41, 144, 61, 59, 224, 119, 135, 238, 14, 193, 149, 124, 228, 39, 107, 208, 243, 180, 7, 177, 21, 88, 19, 5, 225, 3]);
    // ...
}
```
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;

use nekolib::rand_gen;
use nekolib::utils::ascii::*;
use nekolib::utils::rand_gen_macro::*;

rand_gen! {
    rng = ChaCha20Rng::from_seed([250, 120, 31, 164, 15, 176, 41, 144, 61, 59, 224, 119, 135, 238, 14, 193, 149, 124, 228, 39, 107, 208, 243, 180, 7, 177, 21, 88, 19, 5, 225, 3]);

    n in 1_usize..=10;
    a in [1_i64..=100; n];
    s in AsciiString(16) where {
        distribution = &[
            (ASCII_LOWERCASE, 10),
            (ASCII_UPPERCASE, 10),
            (ASCII_DIGIT, 6),
            (charset(b"~!@#$%+?|()^*_-=[]{};:,./"), 5),
        ],
    };
    b in [[[1_u8..100; 3]; 2]; 2];
    p in Permutation(5);
    mut x in 1_i64..10;
}
x += 1;

assert_eq!(a, [32, 86, 41, 68, 66, 46, 56, 82, 40, 1]);
assert_eq!(s, "X52dhjDk%i6)p1F9");
assert_eq!(b, [[[75, 20, 23], [63, 21, 58]], [[12, 6, 57], [51, 95, 70]]]);
assert_eq!(p, [3, 1, 0, 4, 2]);
assert_eq!(x, 5);

oj を用いて、hack をするのに使うとよい。 以下の例は、想定解が Rust で標的が Python のプログラムのもの。

% oj g/i --hack-expected target/release/x --hack-actual 'python3 x-to-be-hacked.py' target/release/x-gen