Skip to content

Commit 689f197

Browse files
committed
rand: deprecate rng.
This should be called far less than it is because it does expensive OS interactions and seeding of the internal RNG, `task_rng` amortises this cost. The main problem is the name is so short and suggestive. The direct equivalent is `StdRng::new`, which does precisely the same thing. The deprecation will make migrating away from the function easier.
1 parent 198caa8 commit 689f197

File tree

8 files changed

+28
-16
lines changed

8 files changed

+28
-16
lines changed

src/libextra/tempfile.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
1313

1414
use std::os;
15-
use rand::Rng;
16-
use rand;
15+
use rand::{task_rng, Rng};
1716
use std::io;
1817
use std::io::fs;
1918

@@ -35,7 +34,7 @@ impl TempDir {
3534
return TempDir::new_in(&abs_tmpdir, suffix);
3635
}
3736

38-
let mut r = rand::rng();
37+
let mut r = task_rng();
3938
for _ in range(0u, 1000) {
4039
let p = tmpdir.join(r.gen_ascii_str(16) + suffix);
4140
match fs::mkdir(&p, io::UserRWX) {

src/libflate/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ mod tests {
9797

9898
#[test]
9999
fn test_flate_round_trip() {
100-
let mut r = rand::rng();
100+
let mut r = rand::task_rng();
101101
let mut words = ~[];
102102
for _ in range(0, 20) {
103103
let range = r.gen_range(1u, 10);

src/librand/lib.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ randomness.
5050
```rust
5151
use rand::Rng;
5252
53-
let mut rng = rand::rng();
53+
let mut rng = rand::task_rng();
5454
if rng.gen() { // bool
5555
println!("int: {}, uint: {}", rng.gen::<int>(), rng.gen::<uint>())
5656
}
@@ -396,6 +396,7 @@ pub trait SeedableRng<Seed>: Rng {
396396
/// operation. If one does not require high performance generation of
397397
/// random numbers, `task_rng` and/or `random` may be more
398398
/// appropriate.
399+
#[deprecated="use `task_rng` or `StdRng::new`"]
399400
pub fn rng() -> StdRng {
400401
StdRng::new()
401402
}
@@ -411,14 +412,26 @@ pub struct StdRng { priv rng: IsaacRng }
411412
pub struct StdRng { priv rng: Isaac64Rng }
412413

413414
impl StdRng {
414-
/// Create a randomly seeded instance of `StdRng`. This reads
415-
/// randomness from the OS to seed the PRNG.
415+
/// Create a randomly seeded instance of `StdRng`.
416+
///
417+
/// This is a very expensive operation as it has to read
418+
/// randomness from the operating system and use this in an
419+
/// expensive seeding operation. If one is only generating a small
420+
/// number of random numbers, or doesn't need the utmost speed for
421+
/// generating each number, `task_rng` and/or `random` may be more
422+
/// appropriate.
416423
#[cfg(not(target_word_size="64"))]
417424
pub fn new() -> StdRng {
418425
StdRng { rng: IsaacRng::new() }
419426
}
420-
/// Create a randomly seeded instance of `StdRng`. This reads
421-
/// randomness from the OS to seed the PRNG.
427+
/// Create a randomly seeded instance of `StdRng`.
428+
///
429+
/// This is a very expensive operation as it has to read
430+
/// randomness from the operating system and use this in an
431+
/// expensive seeding operation. If one is only generating a small
432+
/// number of random numbers, or doesn't need the utmost speed for
433+
/// generating each number, `task_rng` and/or `random` may be more
434+
/// appropriate.
422435
#[cfg(target_word_size="64")]
423436
pub fn new() -> StdRng {
424437
StdRng { rng: Isaac64Rng::new() }

src/libstd/os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@ mod tests {
14091409
}
14101410

14111411
fn make_rand_name() -> ~str {
1412-
let mut rng = rand::rng();
1412+
let mut rng = rand::task_rng();
14131413
let n = ~"TEST" + rng.gen_ascii_str(10u);
14141414
assert!(getenv(n).is_none());
14151415
n

src/libsyntax/parse/token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ pub fn fresh_name(src: &ast::Ident) -> Name {
670670
// following: debug version. Could work in final except that it's incompatible with
671671
// good error messages and uses of struct names in ambiguous could-be-binding
672672
// locations. Also definitely destroys the guarantee given above about ptr_eq.
673-
/*let num = rand::rng().gen_uint_range(0,0xffff);
673+
/*let num = rand::task_rng().gen_uint_range(0,0xffff);
674674
gensym(format!("{}_{}",ident_to_str(src),num))*/
675675
}
676676

src/libuuid/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ mod test {
780780

781781
#[test]
782782
fn test_rand_rand() {
783-
let mut rng = rand::rng();
783+
let mut rng = rand::task_rng();
784784
let u: ~Uuid = rand::Rand::rand(&mut rng);
785785
let ub = u.as_bytes();
786786

src/test/bench/core-std.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn read_line() {
8383
}
8484

8585
fn vec_plus() {
86-
let mut r = rand::rng();
86+
let mut r = rand::task_rng();
8787

8888
let mut v = ~[];
8989
let mut i = 0;
@@ -99,7 +99,7 @@ fn vec_plus() {
9999
}
100100

101101
fn vec_append() {
102-
let mut r = rand::rng();
102+
let mut r = rand::task_rng();
103103

104104
let mut v = ~[];
105105
let mut i = 0;
@@ -116,7 +116,7 @@ fn vec_append() {
116116
}
117117

118118
fn vec_push_all() {
119-
let mut r = rand::rng();
119+
let mut r = rand::task_rng();
120120

121121
let mut v = ~[];
122122
for i in range(0u, 1500) {

src/test/run-pass/morestack6.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn main() {
6767
calllink08,
6868
calllink10
6969
];
70-
let mut rng = rand::rng();
70+
let mut rng = rand::task_rng();
7171
for f in fns.iter() {
7272
let f = *f;
7373
let sz = rng.gen::<u32>() % 256u32 + 256u32;

0 commit comments

Comments
 (0)