Skip to content

Commit e333677

Browse files
committed
---
yaml --- r: 145828 b: refs/heads/try2 c: 71addde h: refs/heads/master v: v3
1 parent 9c6b2e0 commit e333677

File tree

3 files changed

+43
-53
lines changed

3 files changed

+43
-53
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 5bb5f7678530c32caa11a1bc31e27187ff74bc19
8+
refs/heads/try2: 71addded64548ff845d9ef3852a2c1d2592ae39f
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/rand/isaac.rs

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
//! The ISAAC random number generator.
1212
13-
use rand::{seed, Rng, SeedableRng};
13+
use cast;
14+
use rand::{Rng, SeedableRng, OSRng};
1415
use iter::{Iterator, range, range_step, Repeat};
1516
use option::{None, Some};
1617

@@ -29,23 +30,31 @@ pub struct IsaacRng {
2930
priv b: u32,
3031
priv c: u32
3132
}
33+
static EMPTY: IsaacRng = IsaacRng {
34+
cnt: 0,
35+
rsl: [0, .. RAND_SIZE],
36+
mem: [0, .. RAND_SIZE],
37+
a: 0, b: 0, c: 0
38+
};
3239

3340
impl IsaacRng {
3441
/// Create an ISAAC random number generator with a random seed.
3542
pub fn new() -> IsaacRng {
36-
let s = unsafe {seed::<u32>(RAND_SIZE as uint)};
37-
SeedableRng::from_seed(s.as_slice())
43+
let mut rng = EMPTY;
44+
45+
{
46+
let bytes = unsafe {cast::transmute::<&mut [u32], &mut [u8]>(rng.rsl)};
47+
OSRng::new().fill_bytes(bytes);
48+
}
49+
50+
rng.init(true);
51+
rng
3852
}
3953

4054
/// Create an ISAAC random number generator using the default
4155
/// fixed seed.
4256
pub fn new_unseeded() -> IsaacRng {
43-
let mut rng = IsaacRng {
44-
cnt: 0,
45-
rsl: [0, .. RAND_SIZE],
46-
mem: [0, .. RAND_SIZE],
47-
a: 0, b: 0, c: 0
48-
};
57+
let mut rng = EMPTY;
4958
rng.init(false);
5059
rng
5160
}
@@ -193,15 +202,8 @@ impl<'self> SeedableRng<&'self [u32]> for IsaacRng {
193202
/// constructed with a given seed will generate the same sequence
194203
/// of values as all other generators constructed with that seed.
195204
fn from_seed(seed: &'self [u32]) -> IsaacRng {
196-
let mut rng = IsaacRng {
197-
cnt: 0,
198-
rsl: [0, .. RAND_SIZE],
199-
mem: [0, .. RAND_SIZE],
200-
a: 0, b: 0, c: 0
201-
};
202-
205+
let mut rng = EMPTY;
203206
rng.reseed(seed);
204-
205207
rng
206208
}
207209
}
@@ -224,23 +226,30 @@ pub struct Isaac64Rng {
224226
priv c: u64,
225227
}
226228

229+
static EMPTY_64: Isaac64Rng = Isaac64Rng {
230+
cnt: 0,
231+
rsl: [0, .. RAND_SIZE_64],
232+
mem: [0, .. RAND_SIZE_64],
233+
a: 0, b: 0, c: 0,
234+
};
235+
227236
impl Isaac64Rng {
228237
/// Create a 64-bit ISAAC random number generator with a random
229238
/// seed.
230239
pub fn new() -> Isaac64Rng {
231-
let s = unsafe {seed::<u64>(RAND_SIZE_64)};
232-
SeedableRng::from_seed(s.as_slice())
240+
let mut rng = EMPTY_64;
241+
{
242+
let bytes = unsafe {cast::transmute::<&mut [u64], &mut [u8]>(rng.rsl)};
243+
OSRng::new().fill_bytes(bytes);
244+
}
245+
rng.init(true);
246+
rng
233247
}
234248

235249
/// Create a 64-bit ISAAC random number generator using the
236250
/// default fixed seed.
237251
pub fn new_unseeded() -> Isaac64Rng {
238-
let mut rng = Isaac64Rng {
239-
cnt: 0,
240-
rsl: [0, .. RAND_SIZE_64],
241-
mem: [0, .. RAND_SIZE_64],
242-
a: 0, b: 0, c: 0,
243-
};
252+
let mut rng = EMPTY_64;
244253
rng.init(false);
245254
rng
246255
}
@@ -388,12 +397,7 @@ impl<'self> SeedableRng<&'self [u64]> for Isaac64Rng {
388397
/// constructed with a given seed will generate the same sequence
389398
/// of values as all other generators constructed with that seed.
390399
fn from_seed(seed: &'self [u64]) -> Isaac64Rng {
391-
let mut rng = Isaac64Rng {
392-
cnt: 0,
393-
rsl: [0, .. RAND_SIZE_64],
394-
mem: [0, .. RAND_SIZE_64],
395-
a: 0, b: 0, c: 0,
396-
};
400+
let mut rng = EMPTY_64;
397401
rng.reseed(seed);
398402
rng
399403
}
@@ -402,21 +406,21 @@ impl<'self> SeedableRng<&'self [u64]> for Isaac64Rng {
402406
#[cfg(test)]
403407
mod test {
404408
use super::*;
405-
use rand::{Rng, SeedableRng, seed};
409+
use rand::{Rng, SeedableRng, OSRng};
406410
use option::Some;
407411
use iter::range;
408412
use vec;
409413

410414
#[test]
411415
fn test_rng_32_rand_seeded() {
412-
let s = unsafe {seed::<u32>(256)};
416+
let s = OSRng::new().gen_vec::<u32>(256);
413417
let mut ra: IsaacRng = SeedableRng::from_seed(s.as_slice());
414418
let mut rb: IsaacRng = SeedableRng::from_seed(s.as_slice());
415419
assert_eq!(ra.gen_ascii_str(100u), rb.gen_ascii_str(100u));
416420
}
417421
#[test]
418422
fn test_rng_64_rand_seeded() {
419-
let s = unsafe {seed::<u64>(256)};
423+
let s = OSRng::new().gen_vec::<u64>(256);
420424
let mut ra: Isaac64Rng = SeedableRng::from_seed(s.as_slice());
421425
let mut rb: Isaac64Rng = SeedableRng::from_seed(s.as_slice());
422426
assert_eq!(ra.gen_ascii_str(100u), rb.gen_ascii_str(100u));
@@ -439,7 +443,7 @@ mod test {
439443

440444
#[test]
441445
fn test_rng_32_reseed() {
442-
let s = unsafe {seed::<u32>(256)};
446+
let s = OSRng::new().gen_vec::<u32>(256);
443447
let mut r: IsaacRng = SeedableRng::from_seed(s.as_slice());
444448
let string1 = r.gen_ascii_str(100);
445449

@@ -450,7 +454,7 @@ mod test {
450454
}
451455
#[test]
452456
fn test_rng_64_reseed() {
453-
let s = unsafe {seed::<u64>(256)};
457+
let s = OSRng::new().gen_vec::<u64>(256);
454458
let mut r: Isaac64Rng = SeedableRng::from_seed(s.as_slice());
455459
let string1 = r.gen_ascii_str(100);
456460

branches/try2/src/libstd/rand/mod.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -672,20 +672,6 @@ impl XorShiftRng {
672672
}
673673
}
674674

675-
/// Create a new random seed of length `n`. This should only be used
676-
/// to create types for which *any* bit pattern is valid.
677-
pub unsafe fn seed<T: Clone>(n: uint) -> ~[T] {
678-
use unstable::intrinsics;
679-
let mut s = vec::from_elem(n, intrinsics::init());
680-
let mut r = OSRng::new();
681-
682-
{
683-
let s_u8 = cast::transmute::<&mut [T], &mut [u8]>(s);
684-
r.fill_bytes(s_u8);
685-
}
686-
s
687-
}
688-
689675
/// Controls how the task-local RNG is reseeded.
690676
enum TaskRngReseeder {
691677
/// Reseed using the StdRng::new() function, i.e. reading new
@@ -994,15 +980,15 @@ mod test {
994980

995981
#[test]
996982
fn test_std_rng_seeded() {
997-
let s = unsafe {seed::<uint>(256)};
983+
let s = OSRng::new().gen_vec::<uint>(256);
998984
let mut ra: StdRng = SeedableRng::from_seed(s.as_slice());
999985
let mut rb: StdRng = SeedableRng::from_seed(s.as_slice());
1000986
assert_eq!(ra.gen_ascii_str(100u), rb.gen_ascii_str(100u));
1001987
}
1002988

1003989
#[test]
1004990
fn test_std_rng_reseed() {
1005-
let s = unsafe {seed::<uint>(256)};
991+
let s = OSRng::new().gen_vec::<uint>(256);
1006992
let mut r: StdRng = SeedableRng::from_seed(s.as_slice());
1007993
let string1 = r.gen_ascii_str(100);
1008994

0 commit comments

Comments
 (0)