Skip to content

Commit 02750aa

Browse files
committed
Isaac*Rng: replace new_from_u64 with seed_from_u64
1 parent fff888a commit 02750aa

File tree

4 files changed

+68
-31
lines changed

4 files changed

+68
-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/deprecated.rs

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ impl SeedableRng for IsaacRng {
5252
fn from_seed(seed: Self::Seed) -> Self {
5353
IsaacRng(prng::IsaacRng::from_seed(seed))
5454
}
55+
56+
fn seed_from_u64(seed: u64) -> Self {
57+
IsaacRng(prng::IsaacRng::seed_from_u64(seed))
58+
}
5559

5660
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
5761
prng::IsaacRng::from_rng(rng).map(IsaacRng)
@@ -99,6 +103,10 @@ impl SeedableRng for Isaac64Rng {
99103
Isaac64Rng(prng::Isaac64Rng::from_seed(seed))
100104
}
101105

106+
fn seed_from_u64(seed: u64) -> Self {
107+
Isaac64Rng(prng::Isaac64Rng::seed_from_u64(seed))
108+
}
109+
102110
fn from_rng<R: RngCore>(rng: R) -> Result<Self, Error> {
103111
prng::Isaac64Rng::from_rng(rng).map(Isaac64Rng)
104112
}

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))
@@ -130,8 +137,9 @@ impl IsaacRng {
130137
/// Create an ISAAC random number generator using an `u64` as seed.
131138
/// If `seed == 0` this will produce the same stream of random numbers as
132139
/// the reference implementation when used unseeded.
140+
#[deprecated(since="0.6.0", note="use SeedableRng::seed_from_u64 instead")]
133141
pub fn new_from_u64(seed: u64) -> Self {
134-
IsaacRng(BlockRng::new(IsaacCore::new_from_u64(seed)))
142+
Self::seed_from_u64(seed)
135143
}
136144
}
137145

@@ -302,22 +310,6 @@ impl IsaacCore {
302310

303311
Self { mem, a: w(0), b: w(0), c: w(0) }
304312
}
305-
306-
/// Create an ISAAC random number generator using an `u64` as seed.
307-
/// If `seed == 0` this will produce the same stream of random numbers as
308-
/// the reference implementation when used unseeded.
309-
fn new_from_u64(seed: u64) -> Self {
310-
let mut key = [w(0); RAND_SIZE];
311-
key[0] = w(seed as u32);
312-
key[1] = w((seed >> 32) as u32);
313-
// Initialize with only one pass.
314-
// A second pass does not improve the quality here, because all of the
315-
// seed was already available in the first round.
316-
// Not doing the second pass has the small advantage that if
317-
// `seed == 0` this method produces exactly the same state as the
318-
// reference implementation when used unseeded.
319-
Self::init(key, 1)
320-
}
321313
}
322314

323315
impl SeedableRng for IsaacCore {
@@ -333,6 +325,22 @@ impl SeedableRng for IsaacCore {
333325
}
334326
Self::init(seed_extended, 2)
335327
}
328+
329+
/// Create an ISAAC random number generator using an `u64` as seed.
330+
/// If `seed == 0` this will produce the same stream of random numbers as
331+
/// the reference implementation when used unseeded.
332+
fn seed_from_u64(seed: u64) -> Self {
333+
let mut key = [w(0); RAND_SIZE];
334+
key[0] = w(seed as u32);
335+
key[1] = w((seed >> 32) as u32);
336+
// Initialize with only one pass.
337+
// A second pass does not improve the quality here, because all of the
338+
// seed was already available in the first round.
339+
// Not doing the second pass has the small advantage that if
340+
// `seed == 0` this method produces exactly the same state as the
341+
// reference implementation when used unseeded.
342+
Self::init(key, 1)
343+
}
336344

337345
fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, Error> {
338346
// Custom `from_rng` implementation that fills a seed with the same size
@@ -426,11 +434,11 @@ mod test {
426434
#[test]
427435
fn test_isaac_new_uninitialized() {
428436
// Compare the results from initializing `IsaacRng` with
429-
// `new_from_u64(0)`, to make sure it is the same as the reference
437+
// `seed_from_u64(0)`, to make sure it is the same as the reference
430438
// implementation when used uninitialized.
431439
// Note: We only test the first 16 integers, not the full 256 of the
432440
// first block.
433-
let mut rng = IsaacRng::new_from_u64(0);
441+
let mut rng = IsaacRng::seed_from_u64(0);
434442
let mut results = [0u32; 16];
435443
for i in results.iter_mut() { *i = rng.next_u32(); }
436444
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
}
@@ -120,8 +127,9 @@ impl Isaac64Rng {
120127
/// Create an ISAAC-64 random number generator using an `u64` as seed.
121128
/// If `seed == 0` this will produce the same stream of random numbers as
122129
/// the reference implementation when used unseeded.
130+
#[deprecated(since="0.6.0", note="use SeedableRng::seed_from_u64 instead")]
123131
pub fn new_from_u64(seed: u64) -> Self {
124-
Isaac64Rng(BlockRng64::new(Isaac64Core::new_from_u64(seed)))
132+
Self::seed_from_u64(seed)
125133
}
126134
}
127135

@@ -271,16 +279,9 @@ impl Isaac64Core {
271279
/// Create an ISAAC-64 random number generator using an `u64` as seed.
272280
/// If `seed == 0` this will produce the same stream of random numbers as
273281
/// the reference implementation when used unseeded.
282+
#[deprecated(since="0.6.0", note="use SeedableRng::seed_from_u64 instead")]
274283
pub fn new_from_u64(seed: u64) -> Self {
275-
let mut key = [w(0); RAND_SIZE];
276-
key[0] = w(seed);
277-
// Initialize with only one pass.
278-
// A second pass does not improve the quality here, because all of the
279-
// seed was already available in the first round.
280-
// Not doing the second pass has the small advantage that if
281-
// `seed == 0` this method produces exactly the same state as the
282-
// reference implementation when used unseeded.
283-
Self::init(key, 1)
284+
Self::seed_from_u64(seed)
284285
}
285286
}
286287

@@ -297,6 +298,18 @@ impl SeedableRng for Isaac64Core {
297298
}
298299
Self::init(seed_extended, 2)
299300
}
301+
302+
fn seed_from_u64(seed: u64) -> Self {
303+
let mut key = [w(0); RAND_SIZE];
304+
key[0] = w(seed);
305+
// Initialize with only one pass.
306+
// A second pass does not improve the quality here, because all of the
307+
// seed was already available in the first round.
308+
// Not doing the second pass has the small advantage that if
309+
// `seed == 0` this method produces exactly the same state as the
310+
// reference implementation when used unseeded.
311+
Self::init(key, 1)
312+
}
300313

301314
fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, Error> {
302315
// Custom `from_rng` implementation that fills a seed with the same size
@@ -416,11 +429,11 @@ mod test {
416429
#[test]
417430
fn test_isaac64_new_uninitialized() {
418431
// Compare the results from initializing `IsaacRng` with
419-
// `new_from_u64(0)`, to make sure it is the same as the reference
432+
// `seed_from_u64(0)`, to make sure it is the same as the reference
420433
// implementation when used uninitialized.
421434
// Note: We only test the first 16 integers, not the full 256 of the
422435
// first block.
423-
let mut rng = Isaac64Rng::new_from_u64(0);
436+
let mut rng = Isaac64Rng::seed_from_u64(0);
424437
let mut results = [0u64; 16];
425438
for i in results.iter_mut() { *i = rng.next_u64(); }
426439
let expected: [u64; 16] = [

0 commit comments

Comments
 (0)