From d9a89c8f13be1db2229a7a02b4a940a121b5a1b8 Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Mon, 30 Oct 2023 21:24:34 +0100 Subject: [PATCH 1/4] Add example for initializing a PCG RNG (#1347) --- rand_pcg/src/lib.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/rand_pcg/src/lib.rs b/rand_pcg/src/lib.rs index 341313954e7..c4228edfc6c 100644 --- a/rand_pcg/src/lib.rs +++ b/rand_pcg/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2018 Developers of the Rand project. +// Copyright 2018-2023 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 or the MIT license @@ -27,6 +27,18 @@ //! Both of these use 16 bytes of state and 128-bit seeds, and are considered //! value-stable (i.e. any change affecting the output given a fixed seed would //! be considered a breaking change to the crate). +//! +//! # Example +//! +//! To initialize a generator, use the [`SeedableRng`][rand_core::SeedableRng] trait: +//! +//! ```rust +//! use rand::{SeedableRng, Rng}; +//! use rand_pcg::Pcg64Mcg; +//! +//! let mut rng = Pcg64Mcg::from_entropy(); +//! let x: u32 = rng.gen(); +//! ``` #![doc( html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png", From 1924e120cc77aac353adaae9c3a012ce2218ab8e Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Mon, 27 Nov 2023 21:52:23 +0100 Subject: [PATCH 2/4] Fix doc test to work without `rand` and `from_entropy` --- rand_pcg/src/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rand_pcg/src/lib.rs b/rand_pcg/src/lib.rs index c4228edfc6c..86484f7633b 100644 --- a/rand_pcg/src/lib.rs +++ b/rand_pcg/src/lib.rs @@ -33,11 +33,12 @@ //! To initialize a generator, use the [`SeedableRng`][rand_core::SeedableRng] trait: //! //! ```rust -//! use rand::{SeedableRng, Rng}; +//! use rand_core::{SeedableRng, RngCore}; //! use rand_pcg::Pcg64Mcg; //! -//! let mut rng = Pcg64Mcg::from_entropy(); -//! let x: u32 = rng.gen(); +//! let mut rng = Pcg64Mcg::seed_from_u64(0); +//! // Alternatively, you may use `Pcg64Mcg::from_entropy()`. +//! let x: u32 = rng.next_u32(); //! ``` #![doc( From 1cc3f881a1368cbe936ae3d8627ee316b237ce3e Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Mon, 27 Nov 2023 21:54:34 +0100 Subject: [PATCH 3/4] Update changelog --- rand_pcg/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/rand_pcg/CHANGELOG.md b/rand_pcg/CHANGELOG.md index 8bc112adabd..63a37781707 100644 --- a/rand_pcg/CHANGELOG.md +++ b/rand_pcg/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - Add `Lcg128CmDxsm64` generator compatible with NumPy's `PCG64DXSM` (#1202) +- Add examples for initializing the RNGs ## [0.3.1] - 2021-06-15 - Add `advance` methods to RNGs (#1111) From 14d036c60cb4e26ed297204e5d84701dcd501110 Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg Date: Thu, 14 Dec 2023 22:05:17 +0100 Subject: [PATCH 4/4] Add example for using rand (#1347) --- rand_pcg/Cargo.toml | 1 + rand_pcg/src/lib.rs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/rand_pcg/Cargo.toml b/rand_pcg/Cargo.toml index 1d4e811a869..757cc8e326c 100644 --- a/rand_pcg/Cargo.toml +++ b/rand_pcg/Cargo.toml @@ -26,6 +26,7 @@ rand_core = { path = "../rand_core", version = "0.7.0" } serde = { version = "1", features = ["derive"], optional = true } [dev-dependencies] +rand = { path = "..", version = "0.9" } # This is for testing serde, unfortunately we can't specify feature-gated dev # deps yet, see: https://github.com/rust-lang/cargo/issues/1596 # Versions prior to 1.1.4 had incorrect minimal dependencies. diff --git a/rand_pcg/src/lib.rs b/rand_pcg/src/lib.rs index 86484f7633b..c1484c2d55e 100644 --- a/rand_pcg/src/lib.rs +++ b/rand_pcg/src/lib.rs @@ -32,14 +32,24 @@ //! //! To initialize a generator, use the [`SeedableRng`][rand_core::SeedableRng] trait: //! -//! ```rust +//! ``` //! use rand_core::{SeedableRng, RngCore}; //! use rand_pcg::Pcg64Mcg; //! //! let mut rng = Pcg64Mcg::seed_from_u64(0); -//! // Alternatively, you may use `Pcg64Mcg::from_entropy()`. //! let x: u32 = rng.next_u32(); //! ``` +//! +//! The functionality of this crate is implemented using traits from the `rand_core` crate, but you may use the `rand` +//! crate for further functionality to initialize the generator from various sources and to generate random values: +//! +//! ``` +//! use rand::{Rng, SeedableRng}; +//! use rand_pcg::Pcg64Mcg; +//! +//! let mut rng = Pcg64Mcg::from_entropy(); +//! let x: f64 = rng.gen(); +//! ``` #![doc( html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",