Skip to content

Commit 3f4fa93

Browse files
committed
---
yaml --- r: 95278 b: refs/heads/dist-snap c: f39a215 h: refs/heads/master v: v3
1 parent fc32763 commit 3f4fa93

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: c274a6888410ce3e357e014568b43310ed787d36
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
9-
refs/heads/dist-snap: 39a69d323da95ce642ea7fe8d40eb8bdd6a277c8
9+
refs/heads/dist-snap: f39a215f270bc8c958a19cc9f693720232340cbc
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1212
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/src/libstd/rand/mod.rs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,46 @@ pub trait Rng {
643643
/// available in Rust. If you require a specifically seeded `Rng` for
644644
/// consistency over time you should pick one algorithm and create the
645645
/// `Rng` yourself.
646-
pub fn rng() -> IsaacRng {
647-
IsaacRng::new()
646+
///
647+
/// This is a very expensive operation as it has to read randomness
648+
/// from the operating system and use this in an expensive seeding
649+
/// operation. If one does not require high performance, `task_rng`
650+
/// and/or `random` may be more appropriate.
651+
pub fn rng() -> StdRng {
652+
StdRng::new()
653+
}
654+
655+
/// The standard RNG. This is designed to be efficient on the current
656+
/// platform.
657+
#[cfg(not(target_word_size="64"))]
658+
pub struct StdRng { priv rng: IsaacRng }
659+
660+
/// The standard RNG. This is designed to be efficient on the current
661+
/// platform.
662+
#[cfg(target_word_size="64")]
663+
pub struct StdRng { priv rng: Isaac64Rng }
664+
665+
impl StdRng {
666+
#[cfg(not(target_word_size="64"))]
667+
fn new() -> StdRng {
668+
StdRng { rng: IsaacRng::new() }
669+
}
670+
#[cfg(target_word_size="64")]
671+
fn new() -> StdRng {
672+
StdRng { rng: Isaac64Rng::new() }
673+
}
674+
}
675+
676+
impl Rng for StdRng {
677+
#[inline]
678+
fn next_u32(&mut self) -> u32 {
679+
self.rng.next_u32()
680+
}
681+
682+
#[inline]
683+
fn next_u64(&mut self) -> u64 {
684+
self.rng.next_u64()
685+
}
648686
}
649687

650688
/// Create a weak random number generator with a default algorithm and seed.
@@ -728,23 +766,23 @@ pub fn seed(n: uint) -> ~[u8] {
728766
}
729767

730768
// used to make space in TLS for a random number generator
731-
local_data_key!(tls_rng_state: @@mut IsaacRng)
769+
local_data_key!(tls_rng_state: @mut StdRng)
732770

733771
/**
734772
* Gives back a lazily initialized task-local random number generator,
735773
* seeded by the system. Intended to be used in method chaining style, ie
736774
* `task_rng().gen::<int>()`.
737775
*/
738776
#[inline]
739-
pub fn task_rng() -> @mut IsaacRng {
777+
pub fn task_rng() -> @mut StdRng {
740778
let r = local_data::get(tls_rng_state, |k| k.map(|&k| *k));
741779
match r {
742780
None => {
743-
let rng = @@mut IsaacRng::new();
781+
let rng = @mut StdRng::new();
744782
local_data::set(tls_rng_state, rng);
745-
*rng
783+
rng
746784
}
747-
Some(rng) => *rng
785+
Some(rng) => rng
748786
}
749787
}
750788

@@ -985,6 +1023,15 @@ mod bench {
9851023
bh.bytes = size_of::<uint>() as u64;
9861024
}
9871025

1026+
#[bench]
1027+
fn rand_std(bh: &mut BenchHarness) {
1028+
let mut rng = StdRng::new();
1029+
do bh.iter {
1030+
rng.gen::<uint>();
1031+
}
1032+
bh.bytes = size_of::<uint>() as u64;
1033+
}
1034+
9881035
#[bench]
9891036
fn rand_shuffle_100(bh: &mut BenchHarness) {
9901037
let mut rng = XorShiftRng::new();

0 commit comments

Comments
 (0)