lineargen 0.1.0

Bit sequence generators based on LFSRs
Documentation
  • Coverage
  • 100%
    50 out of 50 items documented4 out of 48 items with examples
  • Size
  • Source code size: 18.63 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.69 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • elkasztano/lineargen
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • elkasztano

Lineargen

rand compatible bit sequence generators based on Galois linear feedback shift registers.

Due to the compatibility with rand, various kinds of number sequences can be generated. One can also choose elements from slices or shuffle them using the generated bit sequence from the LFSR.

Note that LFSRs are extremely fast and easy to implement, but their statistical properties might not always be the best and they are definitely not cryptographically secure.

Examples

Choose from elements in an array:

use rand::SeedableRng; // 0.8.5
use rand::seq::SliceRandom;
use lineargen::large::Linear128;

fn main() {
    let mut rng = Linear128::seed_from_u64(987654321);

    for _ in 0..100 { rng.clock(); }

    let my_data = ["foo", "bar", "baz", "qux"];

    // choose elements pseudorandomly
    for _ in 0..10 {
        println!("{}", my_data.choose(&mut rng).unwrap());
    }
}

Generate a sequence of f32:

use rand::prelude::*; // 0.8.5
use lineargen::Linear64;

fn main() {
    let mut rng = Linear64::seed_from_u64(0x5EED5EED5EED);

    for _ in 0..128 { rng.clock(); }

    for _ in 0..20 {
        let my_float: f32 = rng.gen();
        println!("{}", my_float);
    }
}

See how the LFSR actually works:

use rand_core::SeedableRng; // 0.6.4
use lineargen::Linear16;

fn main() {
    let mut rng = Linear16::seed_from_u64(1023);

    for _ in 0..50 {
        rng.clock();
        println!("{:016b}", rng.dump_state());
    }
}

Reference

The LFSR taps were taken from: https://www.physics.otago.ac.nz/reports/electronics/ETR2012-1.pdf.