1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! The implementations of `Rand` for the built-in types.
14 use core::prelude::v1::*;
24 fn rand<R: Rng>(rng: &mut R) -> isize {
25 if isize::BITS == 32 {
26 rng.gen::<i32>() as isize
28 rng.gen::<i64>() as isize
35 fn rand<R: Rng>(rng: &mut R) -> i8 {
42 fn rand<R: Rng>(rng: &mut R) -> i16 {
49 fn rand<R: Rng>(rng: &mut R) -> i32 {
56 fn rand<R: Rng>(rng: &mut R) -> i64 {
63 fn rand<R: Rng>(rng: &mut R) -> usize {
64 if usize::BITS == 32 {
65 rng.gen::<u32>() as usize
67 rng.gen::<u64>() as usize
74 fn rand<R: Rng>(rng: &mut R) -> u8 {
81 fn rand<R: Rng>(rng: &mut R) -> u16 {
88 fn rand<R: Rng>(rng: &mut R) -> u32 {
95 fn rand<R: Rng>(rng: &mut R) -> u64 {
100 macro_rules! float_impls {
101 ($mod_name:ident, $ty:ty, $mantissa_bits:expr, $method_name:ident) => {
103 use {Rand, Rng, Open01, Closed01};
105 const SCALE: $ty = (1u64 << $mantissa_bits) as $ty;
108 /// Generate a floating point number in the half-open
109 /// interval `[0,1)`.
111 /// See `Closed01` for the closed interval `[0,1]`,
112 /// and `Open01` for the open interval `(0,1)`.
114 fn rand<R: Rng>(rng: &mut R) -> $ty {
118 impl Rand for Open01<$ty> {
120 fn rand<R: Rng>(rng: &mut R) -> Open01<$ty> {
121 // add a small amount (specifically 2 bits below
122 // the precision of f64/f32 at 1.0), so that small
123 // numbers are larger than 0, but large numbers
124 // aren't pushed to/above 1.
125 Open01(rng.$method_name() + 0.25 / SCALE)
128 impl Rand for Closed01<$ty> {
130 fn rand<R: Rng>(rng: &mut R) -> Closed01<$ty> {
131 // rescale so that 1.0 - epsilon becomes 1.0
133 Closed01(rng.$method_name() * SCALE / (SCALE - 1.0))
139 float_impls! { f64_rand_impls, f64, 53, next_f64 }
140 float_impls! { f32_rand_impls, f32, 24, next_f32 }
144 fn rand<R: Rng>(rng: &mut R) -> char {
146 const CHAR_MASK: u32 = 0x001f_ffff;
148 // Rejection sampling. About 0.2% of numbers with at most
149 // 21-bits are invalid codepoints (surrogates), so this
150 // will succeed first go almost every time.
151 match char::from_u32(rng.next_u32() & CHAR_MASK) {
161 fn rand<R: Rng>(rng: &mut R) -> bool {
162 rng.gen::<u8>() & 1 == 1
166 macro_rules! tuple_impl {
167 // use variables to indicate the arity of the tuple
168 ($($tyvar:ident),* ) => {
169 // the trailing commas are for the 1 tuple
172 > Rand for ( $( $tyvar ),* , ) {
175 fn rand<R: Rng>(_rng: &mut R) -> ( $( $tyvar ),* , ) {
177 // use the $tyvar's to get the appropriate number of
178 // repeats (they're not actually needed)
191 fn rand<R: Rng>(_: &mut R) -> () { () }
196 tuple_impl!{A, B, C, D}
197 tuple_impl!{A, B, C, D, E}
198 tuple_impl!{A, B, C, D, E, F}
199 tuple_impl!{A, B, C, D, E, F, G}
200 tuple_impl!{A, B, C, D, E, F, G, H}
201 tuple_impl!{A, B, C, D, E, F, G, H, I}
202 tuple_impl!{A, B, C, D, E, F, G, H, I, J}
203 tuple_impl!{A, B, C, D, E, F, G, H, I, J, K}
204 tuple_impl!{A, B, C, D, E, F, G, H, I, J, K, L}
206 impl<T:Rand> Rand for Option<T> {
208 fn rand<R: Rng>(rng: &mut R) -> Option<T> {