Coder Social home page Coder Social logo

chacha-rng-swift's Introduction

Swift

chacha-rng-swift

ChaCha-based RandomNumberGenerator. Compatible with Rust’s rand_chacha.

Usage

CSRNG

You should almost certainly use SystemRandomNumberGenerator instead.

import ChaChaRNG

var rng = ChaCha8RNG() // Seeded via SystemRandomNumberGenerator, stream: 0.

SIMD2.random(in: 0..<1234, using: &rng)

CSPRNG

var rng = ChaCha8RNG(seed: .zero) // All-zero seed, stream: 0.

Int.random(in: 0..<1234, using: &rng) // 1032

Individual unsigned integers

var rng = ChaCha20RNG(seed: .zero)

rng.next() as UInt8  // 118
rng.next() as UInt16 // 61856
rng.next() as UInt32 // 3848953152
rng.next() as UInt64 // 13265865887270667859

Equivalent Rust:

let mut rng = ChaChaRng::from_seed([0u8; 32]);

println!("{}", rng.gen::<u8>());  // 118
println!("{}", rng.gen::<u16>()); // 61856
println!("{}", rng.gen::<u32>()); // 3848953152
println!("{}", rng.gen::<u64>()); // 13265865887270667859

Individual floating-point numbers

var rng = ChaCha20RNG(seed: .zero)

rng.next() as Float16 // Only supported on some hardware.
rng.next() as Float32 // 0.56344515
rng.next() as Float64 // 0.15914191768880792

Equivalent Rust:

let mut rng = ChaChaRng::from_seed([0u8; 32]);

println!("{}", rng.gen::<f32>()); // 0.6792102
println!("{}", rng.gen::<f32>()); // 0.56344515
println!("{}", rng.gen::<f64>()); // 0.15914191768880792

Fill buffers:

var rng = ChaCha20RNG(seed: .zero)
        
var array = [UInt8](repeating: 0, count: 5)
array.withUnsafeMutableBytes {
    rng.fill($0)
}
array // [118, 184, 224, 173, 160]

Equivalent Rust:

let mut rng = ChaChaRng::from_seed([0u8; 32]);

let mut array = [0u8; 5];
rng.fill(&mut a[..]);
println!("{:?}", array); // [118, 184, 224, 173, 160]

chacha-rng-swift's People

Contributors

dependabot[bot] avatar nixberg avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.