Skip to content

Commit 3c2e82f

Browse files
authored
Merge pull request #1352 from vks/pcg-example
Add example for initializing a PCG RNG
2 parents e0292f3 + 14d036c commit 3c2e82f

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

rand_pcg/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
- Add `Lcg128CmDxsm64` generator compatible with NumPy's `PCG64DXSM` (#1202)
9+
- Add examples for initializing the RNGs
910

1011
## [0.3.1] - 2021-06-15
1112
- Add `advance` methods to RNGs (#1111)

rand_pcg/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ rand_core = { path = "../rand_core", version = "0.7.0" }
2626
serde = { version = "1", features = ["derive"], optional = true }
2727

2828
[dev-dependencies]
29+
rand = { path = "..", version = "0.9" }
2930
# This is for testing serde, unfortunately we can't specify feature-gated dev
3031
# deps yet, see: https://github.com/rust-lang/cargo/issues/1596
3132
# Versions prior to 1.1.4 had incorrect minimal dependencies.

rand_pcg/src/lib.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 Developers of the Rand project.
1+
// Copyright 2018-2023 Developers of the Rand project.
22
//
33
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
44
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
@@ -27,6 +27,29 @@
2727
//! Both of these use 16 bytes of state and 128-bit seeds, and are considered
2828
//! value-stable (i.e. any change affecting the output given a fixed seed would
2929
//! be considered a breaking change to the crate).
30+
//!
31+
//! # Example
32+
//!
33+
//! To initialize a generator, use the [`SeedableRng`][rand_core::SeedableRng] trait:
34+
//!
35+
//! ```
36+
//! use rand_core::{SeedableRng, RngCore};
37+
//! use rand_pcg::Pcg64Mcg;
38+
//!
39+
//! let mut rng = Pcg64Mcg::seed_from_u64(0);
40+
//! let x: u32 = rng.next_u32();
41+
//! ```
42+
//!
43+
//! The functionality of this crate is implemented using traits from the `rand_core` crate, but you may use the `rand`
44+
//! crate for further functionality to initialize the generator from various sources and to generate random values:
45+
//!
46+
//! ```
47+
//! use rand::{Rng, SeedableRng};
48+
//! use rand_pcg::Pcg64Mcg;
49+
//!
50+
//! let mut rng = Pcg64Mcg::from_entropy();
51+
//! let x: f64 = rng.gen();
52+
//! ```
3053
3154
#![doc(
3255
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png",

0 commit comments

Comments
 (0)