Quad-rand implements pseudo-random generator

Related tags

Utilities quad-rand
Overview

quad-rand

Crates.io version Documentation on docs.rs

quad-rand implements pseudo-random generator http://www.pcg-random.org/download.html based on rust atomics.

Compatible with wasm and any other rust target with std.

Basic usage, no dependencies involved:

use quad_rand as qrand;

// seed random
qrand::srand(12345);

// get random number from 0 to u32::MAX
let x = qrand::rand();

// get random number from given range
let x = qrand::gen_range(0., 1.);
assert!(x >= 0. && x < 1.);

// gen_range works for most of standard number types
let x: u8 = qrand::gen_range(64, 128);
assert!(x >= 64 && x < 128);

Optional compatibility layer with rand crate:

use quad_rand::compat::QuadRand;
use rand::seq::SliceRandom;

let mut vec = vec![1, 2, 3, 4, 5, 6];

// QuadRand is rand::RngCore implementation, allowing to use all the cool stuff from rand
vec.shuffle(&mut QuadRand);
Comments
  • several minor improvements

    several minor improvements

    most important:

    • include all features in docs.rs (compat module didn't show)
    • create both examples adapting them from the readme
    • impl RandomRange for u16, i8 & isize

    also:

    • fix max values on several RandomRange impls, homogenize & rearrange them
    • reorganize and simplify Cargo.toml
    • fix a typo in the readme
    • run rustfmt

    NOTE: I've left u32::MAX as the maximum number on [ui]64 & [ui]size for fear of overflowing, although I'm not sure of the underlying mechanics.

    please let me know if you want something done differently.

    opened by joseluis 1
  • Does not build for WASM

    Does not build for WASM

    cargo build --features rand --target wasm32-unknown-unknown produces

    error: target is not supported, for more information see: https://docs.rs/getrandom/#unsupported-targets
       --> /home/martin/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.2/src/lib.rs:213:9
        |
    213 | /         compile_error!("target is not supported, for more information see: \
    214 | |                         https://docs.rs/getrandom/#unsupported-targets");
        | |_________________________________________________________________________^
    
    error[E0433]: failed to resolve: use of undeclared crate or module `imp`
       --> /home/martin/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.2/src/lib.rs:235:5
        |
    235 |     imp::getrandom_inner(dest)
        |     ^^^ use of undeclared crate or module `imp`
    

    This is because rand compiled for WASM requires the getrandon crate with the js feature enabled - which would pull in wasm-bindgen and the generated WASM would fail to load.

    I think the solution is to use default-features = false with rand and only enable additional features you need.

    opened by martin-t 1
  • Precision issues with gen_range

    Precision issues with gen_range

    Approximately 1 in every 2^25 calls, gen_range will return the value of its "high" parameter, while its output is supposed to be strictly less than high. Part of the problem is that u32 can't be represented exactly in f32, which is why it isn't 1 in 2^32. In the various gen_range implementations, a possible improvement would be to replace this line:

    let r = rand() as f32 / std::u32::MAX as f32;                                   
    

    with:

    let r = rand() as f64 / (std::u32::MAX as f64 + 1f64);                                   
    
    opened by Gerstacker 0
  • Add license file

    Add license file

    Hi. i am in the process to package quad-rand in fedora and they would like to have a license file include in the crate source. https://bugzilla.redhat.com/show_bug.cgi?id=1971208 Here the guideline for license in fedora: https://docs.fedoraproject.org/en-US/packaging-guidelines/LicensingGuidelines/#_license_text

    opened by ghost 0
  • The `shuffle` function never shuffles the first element into the first spot.

    The `shuffle` function never shuffles the first element into the first spot.

    Running the code:

    use macroquad::{logging as log, rand::ChooseRandom};
    
    #[macroquad::main(window_conf)]
    async fn main() {
        let mut results = [0;4];
        for _ in 0..100 {
            let mut test = vec![0,1,2,3];
            test.shuffle();
            results[test[0]] += 1;
        }
        log::info!("results: {:?}", results);
    }
    

    consistently gives the output:

    results: [0, 39, 33, 28]
    

    whereas I'd expect something more like:

    results: [25, 26, 25, 24]
    
    opened by bwinton 1
  • QuadRand should store RNG state internally

    QuadRand should store RNG state internally

    If there are 2 separate QuadRand objects, they should behave as separate RNGs - if i am reading the code right, they would currently use the same global state.

    opened by martin-t 1
Owner
Fedor Logachev
Fedor Logachev
Pure rust implementation of python's random module with compatible generator behaviour.

pyrand Pure rust implementation of (parts of) python's random module with compatible PRNG behaviour: seeding with equivalent values will yield identic

Stefan V. 4 Feb 10, 2024
This crate implements an array_chunks method for iterators

This crate implements an array_chunks method for iterators. It behaves like [slice::array_chunks] but works with any [Iterator] type. Several nightly

kangalioo 1 Jan 24, 2022
A Rust library that implements the main lightning logic of the lipa wallet app.

lipa-lightning-lib (3L) Warning This library is not production ready yet. Build Test Start Nigiri Bitcoin: nigiri start --ln The --ln flag is not stri

lipa 9 Dec 15, 2022
ᎩᎦᎨᎢ (IPA: [gigagei]) is a random quote fetching console utility. Written in Rust.

gigagei ᎩᎦᎨᎢ (IPA: [gigagei]) is a random quote fetching console utility. Written in Rust. Installing Use latest pre-built binary from releases Buildi

veleth 10 Jun 17, 2022
SubStrings, Slices and Random String Access in Rust

SubStrings, Slices and Random String Access in Rust This is a simple way to do it. Description Rust string processing is kind of hard, because text in

João Nuno Carvalho 2 Oct 24, 2021
A Rust library for random number generation.

A Rust library for random number generation.

null 1.3k Jan 6, 2023
A random.org client library for Rust

random.org A https://random.org client library. The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random

Victor Polevoy 7 Mar 10, 2022
A simple interpreter for the mathematical random-access machine

Random-access machine runner A simple Rust RAM program runner. Lexer/Parser Program executor Code formatter Web Compiled to WASM to run in the browser

Marcin Wojnarowski 5 Jan 14, 2023
🦀 Rust-based implementation of a Snowflake Generator which communicates using gRPC

Clawflake Clawflake is a Rust application which implements Twitter Snowflakes and communicates using gRPC. Snowflake ID numbers are 63 bits integers s

n1c00o 5 Oct 31, 2022
A simple code boilerplate generator written in Rust.

?? Cgen What is Cgen? A modern, cross-platform, multi-language boilerplate generator aimed to make your code generation less hectic! If you wish to su

Rithul Kamesh 1 Dec 25, 2021
Universal changelog generator using conventional commit+ with monorepo support. Written in Rust.

chlog Universal changelog generator using conventional commit+ with monorepo support. chlog can generate the changelog from the conventional commits w

Jeff Yang 3 Nov 27, 2022
A simple, fast, easy README generator

Welcome to Readme Generator A simple, fast, easy README generator. Never worry about READMEs again! What it does: Run the command in your project's di

Dhravya Shah 41 Nov 13, 2022
A simple quote-based code generator for Rust

flexgen A flexible, yet simple quote-based code generator for creating beautiful Rust code Why? Rust has two types of macros, and they are both very p

Scott Meeuwsen 11 Oct 13, 2022
Boot tools: loader, image generator, etc as a library crate

ArcBoot v0 A uefi bootloader for riscv, arm and x86. Comes in the form of a single executable. the source code contains a single executable target and

Spectral Project 3 Oct 3, 2022
Flexible snowflake generator, reference snoyflake and leaf.

Flexible snowflake generator, reference snoyflake and leaf.

Egccri 2 May 6, 2022
⚙️ A cute .gitignore generator with support for custom templates

Gign A Gitignore Generator Table of Contents Examples Install Custom templates Help Examples # This is how you going to use it probably most of the ti

Vadim 6 Dec 7, 2022
Nexmark event generator in Rust.

Nexmark-rs The Nexmark benchmark data generator in Rust. Installation cargo install nexmark --features bin Usage Generate nexmark events. Print one pe

RisingWave Labs 11 Nov 27, 2022
A code generator to reduce repetitive tasks and build high-quality Rust libraries. 🦀

LibMake A code generator to reduce repetitive tasks and build high-quality Rust libraries Welcome to libmake ?? Website • Documentation • Report Bug •

Sebastien Rousseau 27 Mar 12, 2023
A dynamic image generator.

dynimgen A dynamic image generator. How to use step 1: The designers export the design drawing as an svg file <svg> <rect /> <image src="img.png"

null 29 Jun 14, 2023