Skip to content

Commit 52563b0

Browse files
committed
Isaac*Rng: replace new_from_u64 with seed_from_u64
1 parent 1818b99 commit 52563b0

File tree

3 files changed

+60
-31
lines changed

3 files changed

+60
-31
lines changed

rand_core/src/block.rs

+8
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
287287
Self::new(R::from_seed(seed))
288288
}
289289

290+
fn seed_from_u64(seed: u64) -> Self {
291+
Self::new(R::seed_from_u64(seed))
292+
}
293+
290294
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
291295
Ok(Self::new(R::from_rng(rng)?))
292296
}
@@ -494,6 +498,10 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng64<R> {
494498
Self::new(R::from_seed(seed))
495499
}
496500

501+
fn seed_from_u64(seed: u64) -> Self {
502+
Self::new(R::seed_from_u64(seed))
503+
}
504+
497505
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
498506
Ok(Self::new(R::from_rng(rng)?))
499507
}

src/prng/isaac.rs

+27-19
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ impl SeedableRng for IsaacRng {
120120
fn from_seed(seed: Self::Seed) -> Self {
121121
IsaacRng(BlockRng::<IsaacCore>::from_seed(seed))
122122
}
123+
124+
/// Create an ISAAC random number generator using an `u64` as seed.
125+
/// If `seed == 0` this will produce the same stream of random numbers as
126+
/// the reference implementation when used unseeded.
127+
fn seed_from_u64(seed: u64) -> Self {
128+
IsaacRng(BlockRng::<IsaacCore>::seed_from_u64(seed))
129+
}
123130

124131
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
125132
BlockRng::<IsaacCore>::from_rng(rng).map(|rng| IsaacRng(rng))
@@ -139,8 +146,9 @@ impl IsaacRng {
139146
/// Create an ISAAC random number generator using an `u64` as seed.
140147
/// If `seed == 0` this will produce the same stream of random numbers as
141148
/// the reference implementation when used unseeded.
149+
#[deprecated(since="0.6.0", note="use SeedableRng::seed_from_u64 instead")]
142150
pub fn new_from_u64(seed: u64) -> Self {
143-
IsaacRng(BlockRng::new(IsaacCore::new_from_u64(seed)))
151+
Self::seed_from_u64(seed)
144152
}
145153
}
146154

@@ -311,22 +319,6 @@ impl IsaacCore {
311319

312320
Self { mem, a: w(0), b: w(0), c: w(0) }
313321
}
314-
315-
/// Create an ISAAC random number generator using an `u64` as seed.
316-
/// If `seed == 0` this will produce the same stream of random numbers as
317-
/// the reference implementation when used unseeded.
318-
fn new_from_u64(seed: u64) -> Self {
319-
let mut key = [w(0); RAND_SIZE];
320-
key[0] = w(seed as u32);
321-
key[1] = w((seed >> 32) as u32);
322-
// Initialize with only one pass.
323-
// A second pass does not improve the quality here, because all of the
324-
// seed was already available in the first round.
325-
// Not doing the second pass has the small advantage that if
326-
// `seed == 0` this method produces exactly the same state as the
327-
// reference implementation when used unseeded.
328-
Self::init(key, 1)
329-
}
330322
}
331323

332324
impl SeedableRng for IsaacCore {
@@ -342,6 +334,22 @@ impl SeedableRng for IsaacCore {
342334
}
343335
Self::init(seed_extended, 2)
344336
}
337+
338+
/// Create an ISAAC random number generator using an `u64` as seed.
339+
/// If `seed == 0` this will produce the same stream of random numbers as
340+
/// the reference implementation when used unseeded.
341+
fn seed_from_u64(seed: u64) -> Self {
342+
let mut key = [w(0); RAND_SIZE];
343+
key[0] = w(seed as u32);
344+
key[1] = w((seed >> 32) as u32);
345+
// Initialize with only one pass.
346+
// A second pass does not improve the quality here, because all of the
347+
// seed was already available in the first round.
348+
// Not doing the second pass has the small advantage that if
349+
// `seed == 0` this method produces exactly the same state as the
350+
// reference implementation when used unseeded.
351+
Self::init(key, 1)
352+
}
345353

346354
fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, Error> {
347355
// Custom `from_rng` implementation that fills a seed with the same size
@@ -435,11 +443,11 @@ mod test {
435443
#[test]
436444
fn test_isaac_new_uninitialized() {
437445
// Compare the results from initializing `IsaacRng` with
438-
// `new_from_u64(0)`, to make sure it is the same as the reference
446+
// `seed_from_u64(0)`, to make sure it is the same as the reference
439447
// implementation when used uninitialized.
440448
// Note: We only test the first 16 integers, not the full 256 of the
441449
// first block.
442-
let mut rng = IsaacRng::new_from_u64(0);
450+
let mut rng = IsaacRng::seed_from_u64(0);
443451
let mut results = [0u32; 16];
444452
for i in results.iter_mut() { *i = rng.next_u32(); }
445453
let expected: [u32; 16] = [

src/prng/isaac64.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ impl SeedableRng for Isaac64Rng {
111111
Isaac64Rng(BlockRng64::<Isaac64Core>::from_seed(seed))
112112
}
113113

114+
/// Create an ISAAC random number generator using an `u64` as seed.
115+
/// If `seed == 0` this will produce the same stream of random numbers as
116+
/// the reference implementation when used unseeded.
117+
fn seed_from_u64(seed: u64) -> Self {
118+
Isaac64Rng(BlockRng64::<Isaac64Core>::seed_from_u64(seed))
119+
}
120+
114121
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
115122
BlockRng64::<Isaac64Core>::from_rng(rng).map(|rng| Isaac64Rng(rng))
116123
}
@@ -129,8 +136,9 @@ impl Isaac64Rng {
129136
/// Create an ISAAC-64 random number generator using an `u64` as seed.
130137
/// If `seed == 0` this will produce the same stream of random numbers as
131138
/// the reference implementation when used unseeded.
139+
#[deprecated(since="0.6.0", note="use SeedableRng::seed_from_u64 instead")]
132140
pub fn new_from_u64(seed: u64) -> Self {
133-
Isaac64Rng(BlockRng64::new(Isaac64Core::new_from_u64(seed)))
141+
Self::seed_from_u64(seed)
134142
}
135143
}
136144

@@ -280,16 +288,9 @@ impl Isaac64Core {
280288
/// Create an ISAAC-64 random number generator using an `u64` as seed.
281289
/// If `seed == 0` this will produce the same stream of random numbers as
282290
/// the reference implementation when used unseeded.
291+
#[deprecated(since="0.6.0", note="use SeedableRng::seed_from_u64 instead")]
283292
pub fn new_from_u64(seed: u64) -> Self {
284-
let mut key = [w(0); RAND_SIZE];
285-
key[0] = w(seed);
286-
// Initialize with only one pass.
287-
// A second pass does not improve the quality here, because all of the
288-
// seed was already available in the first round.
289-
// Not doing the second pass has the small advantage that if
290-
// `seed == 0` this method produces exactly the same state as the
291-
// reference implementation when used unseeded.
292-
Self::init(key, 1)
293+
Self::seed_from_u64(seed)
293294
}
294295
}
295296

@@ -306,6 +307,18 @@ impl SeedableRng for Isaac64Core {
306307
}
307308
Self::init(seed_extended, 2)
308309
}
310+
311+
fn seed_from_u64(seed: u64) -> Self {
312+
let mut key = [w(0); RAND_SIZE];
313+
key[0] = w(seed);
314+
// Initialize with only one pass.
315+
// A second pass does not improve the quality here, because all of the
316+
// seed was already available in the first round.
317+
// Not doing the second pass has the small advantage that if
318+
// `seed == 0` this method produces exactly the same state as the
319+
// reference implementation when used unseeded.
320+
Self::init(key, 1)
321+
}
309322

310323
fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, Error> {
311324
// Custom `from_rng` implementation that fills a seed with the same size
@@ -425,11 +438,11 @@ mod test {
425438
#[test]
426439
fn test_isaac64_new_uninitialized() {
427440
// Compare the results from initializing `IsaacRng` with
428-
// `new_from_u64(0)`, to make sure it is the same as the reference
441+
// `seed_from_u64(0)`, to make sure it is the same as the reference
429442
// implementation when used uninitialized.
430443
// Note: We only test the first 16 integers, not the full 256 of the
431444
// first block.
432-
let mut rng = Isaac64Rng::new_from_u64(0);
445+
let mut rng = Isaac64Rng::seed_from_u64(0);
433446
let mut results = [0u64; 16];
434447
for i in results.iter_mut() { *i = rng.next_u64(); }
435448
let expected: [u64; 16] = [

0 commit comments

Comments
 (0)