Skip to content

Commit b140e8c

Browse files
committed
---
yaml --- r: 145824 b: refs/heads/try2 c: 0b1a0d0 h: refs/heads/master v: v3
1 parent 9bc546d commit b140e8c

File tree

3 files changed

+202
-168
lines changed

3 files changed

+202
-168
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: 6f4ec72362c2cf223b85d4fae9a1592a02e80317
8+
refs/heads/try2: 0b1a0d01a8d0a769cc2c0bd9b11bfb71864d2f36
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/mod.rs

Lines changed: 2 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -45,14 +45,11 @@ fn main () {
4545

4646
use cast;
4747
use container::Container;
48-
use int;
4948
use iter::{Iterator, range};
5049
use local_data;
5150
use prelude::*;
5251
use str;
53-
use u32;
5452
use u64;
55-
use uint;
5653
use vec;
5754
use os::getenv;
5855

@@ -64,6 +61,7 @@ pub mod isaac;
6461
pub mod os;
6562
pub mod reader;
6663
pub mod reseeding;
64+
mod rand_impls;
6765

6866
/// A type that can be randomly generated using an Rng
6967
pub trait Rand {
@@ -72,169 +70,6 @@ pub trait Rand {
7270
fn rand<R: Rng>(rng: &mut R) -> Self;
7371
}
7472

75-
impl Rand for int {
76-
#[inline]
77-
fn rand<R: Rng>(rng: &mut R) -> int {
78-
if int::bits == 32 {
79-
rng.gen::<i32>() as int
80-
} else {
81-
rng.gen::<i64>() as int
82-
}
83-
}
84-
}
85-
86-
impl Rand for i8 {
87-
#[inline]
88-
fn rand<R: Rng>(rng: &mut R) -> i8 {
89-
rng.next_u32() as i8
90-
}
91-
}
92-
93-
impl Rand for i16 {
94-
#[inline]
95-
fn rand<R: Rng>(rng: &mut R) -> i16 {
96-
rng.next_u32() as i16
97-
}
98-
}
99-
100-
impl Rand for i32 {
101-
#[inline]
102-
fn rand<R: Rng>(rng: &mut R) -> i32 {
103-
rng.next_u32() as i32
104-
}
105-
}
106-
107-
impl Rand for i64 {
108-
#[inline]
109-
fn rand<R: Rng>(rng: &mut R) -> i64 {
110-
rng.next_u64() as i64
111-
}
112-
}
113-
114-
impl Rand for uint {
115-
#[inline]
116-
fn rand<R: Rng>(rng: &mut R) -> uint {
117-
if uint::bits == 32 {
118-
rng.gen::<u32>() as uint
119-
} else {
120-
rng.gen::<u64>() as uint
121-
}
122-
}
123-
}
124-
125-
impl Rand for u8 {
126-
#[inline]
127-
fn rand<R: Rng>(rng: &mut R) -> u8 {
128-
rng.next_u32() as u8
129-
}
130-
}
131-
132-
impl Rand for u16 {
133-
#[inline]
134-
fn rand<R: Rng>(rng: &mut R) -> u16 {
135-
rng.next_u32() as u16
136-
}
137-
}
138-
139-
impl Rand for u32 {
140-
#[inline]
141-
fn rand<R: Rng>(rng: &mut R) -> u32 {
142-
rng.next_u32()
143-
}
144-
}
145-
146-
impl Rand for u64 {
147-
#[inline]
148-
fn rand<R: Rng>(rng: &mut R) -> u64 {
149-
rng.next_u64()
150-
}
151-
}
152-
153-
impl Rand for f32 {
154-
#[inline]
155-
fn rand<R: Rng>(rng: &mut R) -> f32 {
156-
rng.gen::<f64>() as f32
157-
}
158-
}
159-
160-
static SCALE : f64 = (u32::max_value as f64) + 1.0f64;
161-
impl Rand for f64 {
162-
#[inline]
163-
fn rand<R: Rng>(rng: &mut R) -> f64 {
164-
let u1 = rng.next_u32() as f64;
165-
let u2 = rng.next_u32() as f64;
166-
let u3 = rng.next_u32() as f64;
167-
168-
((u1 / SCALE + u2) / SCALE + u3) / SCALE
169-
}
170-
}
171-
172-
impl Rand for bool {
173-
#[inline]
174-
fn rand<R: Rng>(rng: &mut R) -> bool {
175-
rng.gen::<u8>() & 1 == 1
176-
}
177-
}
178-
179-
macro_rules! tuple_impl {
180-
// use variables to indicate the arity of the tuple
181-
($($tyvar:ident),* ) => {
182-
// the trailing commas are for the 1 tuple
183-
impl<
184-
$( $tyvar : Rand ),*
185-
> Rand for ( $( $tyvar ),* , ) {
186-
187-
#[inline]
188-
fn rand<R: Rng>(_rng: &mut R) -> ( $( $tyvar ),* , ) {
189-
(
190-
// use the $tyvar's to get the appropriate number of
191-
// repeats (they're not actually needed)
192-
$(
193-
_rng.gen::<$tyvar>()
194-
),*
195-
,
196-
)
197-
}
198-
}
199-
}
200-
}
201-
202-
impl Rand for () {
203-
#[inline]
204-
fn rand<R: Rng>(_: &mut R) -> () { () }
205-
}
206-
tuple_impl!{A}
207-
tuple_impl!{A, B}
208-
tuple_impl!{A, B, C}
209-
tuple_impl!{A, B, C, D}
210-
tuple_impl!{A, B, C, D, E}
211-
tuple_impl!{A, B, C, D, E, F}
212-
tuple_impl!{A, B, C, D, E, F, G}
213-
tuple_impl!{A, B, C, D, E, F, G, H}
214-
tuple_impl!{A, B, C, D, E, F, G, H, I}
215-
tuple_impl!{A, B, C, D, E, F, G, H, I, J}
216-
217-
impl<T:Rand> Rand for Option<T> {
218-
#[inline]
219-
fn rand<R: Rng>(rng: &mut R) -> Option<T> {
220-
if rng.gen() {
221-
Some(rng.gen())
222-
} else {
223-
None
224-
}
225-
}
226-
}
227-
228-
impl<T: Rand> Rand for ~T {
229-
#[inline]
230-
fn rand<R: Rng>(rng: &mut R) -> ~T { ~rng.gen() }
231-
}
232-
233-
impl<T: Rand + 'static> Rand for @T {
234-
#[inline]
235-
fn rand<R: Rng>(rng: &mut R) -> @T { @rng.gen() }
236-
}
237-
23873
/// A value with a particular weight compared to other values
23974
pub struct Weighted<T> {
24075
/// The numerical weight of this item

0 commit comments

Comments
 (0)