1 // Copyright 2012 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.
12 Random number generation.
14 The key functions are `random()` and `RngUtil::gen()`. These are polymorphic
15 and so can be used to generate any type that implements `Rand`. Type inference
16 means that often a simple call to `rand::random()` or `rng.gen()` will
17 suffice, but sometimes an annotation is required, e.g. `rand::random::<float>()`.
19 See the `distributions` submodule for sampling random numbers from
20 distributions like normal and exponential.
26 use std::rand::RngUtil;
29 let mut rng = rand::rng();
30 if rng.gen() { // bool
31 printfln!("int: %d, uint: %u", rng.gen(), rng.gen())
40 let tuple_ptr = rand::random::<~(f64, char)>();
49 use container::Container;
51 use iterator::{Iterator, range};
63 #[path="rand/distributions.rs"]
64 pub mod distributions;
66 /// A type that can be randomly generated using an Rng
68 /// Generates a random instance of this type using the specified source of
70 fn rand<R: Rng>(rng: &mut R) -> Self;
75 fn rand<R: Rng>(rng: &mut R) -> int {
79 rng.gen::<i64>() as int
86 fn rand<R: Rng>(rng: &mut R) -> i8 {
93 fn rand<R: Rng>(rng: &mut R) -> i16 {
100 fn rand<R: Rng>(rng: &mut R) -> i32 {
107 fn rand<R: Rng>(rng: &mut R) -> i64 {
108 (rng.next() as i64 << 32) | rng.next() as i64
114 fn rand<R: Rng>(rng: &mut R) -> uint {
115 if uint::bits == 32 {
118 rng.gen::<u64>() as uint
125 fn rand<R: Rng>(rng: &mut R) -> u8 {
132 fn rand<R: Rng>(rng: &mut R) -> u16 {
139 fn rand<R: Rng>(rng: &mut R) -> u32 {
146 fn rand<R: Rng>(rng: &mut R) -> u64 {
147 (rng.next() as u64 << 32) | rng.next() as u64
151 impl Rand for float {
153 fn rand<R: Rng>(rng: &mut R) -> float {
154 rng.gen::<f64>() as float
160 fn rand<R: Rng>(rng: &mut R) -> f32 {
161 rng.gen::<f64>() as f32
165 static SCALE : f64 = (u32::max_value as f64) + 1.0f64;
168 fn rand<R: Rng>(rng: &mut R) -> f64 {
169 let u1 = rng.next() as f64;
170 let u2 = rng.next() as f64;
171 let u3 = rng.next() as f64;
173 ((u1 / SCALE + u2) / SCALE + u3) / SCALE
179 fn rand<R: Rng>(rng: &mut R) -> char {
186 fn rand<R: Rng>(rng: &mut R) -> bool {
187 rng.next() & 1u32 == 1u32
191 macro_rules! tuple_impl {
192 // use variables to indicate the arity of the tuple
193 ($($tyvar:ident),* ) => {
194 // the trailing commas are for the 1 tuple
197 > Rand for ( $( $tyvar ),* , ) {
200 fn rand<R: Rng>(_rng: &mut R) -> ( $( $tyvar ),* , ) {
202 // use the $tyvar's to get the appropriate number of
203 // repeats (they're not actually needed)
216 fn rand<R: Rng>(_: &mut R) -> () { () }
221 tuple_impl!{A, B, C, D}
222 tuple_impl!{A, B, C, D, E}
223 tuple_impl!{A, B, C, D, E, F}
224 tuple_impl!{A, B, C, D, E, F, G}
225 tuple_impl!{A, B, C, D, E, F, G, H}
226 tuple_impl!{A, B, C, D, E, F, G, H, I}
227 tuple_impl!{A, B, C, D, E, F, G, H, I, J}
229 impl<T:Rand> Rand for Option<T> {
231 fn rand<R: Rng>(rng: &mut R) -> Option<T> {
240 impl<T: Rand> Rand for ~T {
242 fn rand<R: Rng>(rng: &mut R) -> ~T { ~rng.gen() }
245 impl<T: Rand + 'static> Rand for @T {
247 fn rand<R: Rng>(rng: &mut R) -> @T { @rng.gen() }
255 pub fn rand_seed_size() -> size_t;
256 pub fn rand_gen_seed(buf: *mut u8, sz: size_t);
260 /// A random number generator
262 /// Return the next random integer
263 pub fn next(&mut self) -> u32;
266 /// A value with a particular weight compared to other values
267 pub struct Weighted<T> {
268 /// The numerical weight of this item
270 /// The actual item which is being weighted
274 /// Helper functions attached to the Rng type
276 /// Return a random value of a Rand type
277 fn gen<T:Rand>(&mut self) -> T;
279 * Return a int randomly chosen from the range [start, end),
280 * failing if start >= end
282 fn gen_int_range(&mut self, start: int, end: int) -> int;
284 * Return a uint randomly chosen from the range [start, end),
285 * failing if start >= end
287 fn gen_uint_range(&mut self, start: uint, end: uint) -> uint;
289 * Return a char randomly chosen from chars, failing if chars is empty
291 fn gen_char_from(&mut self, chars: &str) -> char;
293 * Return a bool with a 1 in n chance of true
300 * use std::rand::RngUtil;
303 * let mut rng = rand::rng();
304 * printfln!("%b", rng.gen_weighted_bool(3));
308 fn gen_weighted_bool(&mut self, n: uint) -> bool;
310 * Return a random string of the specified length composed of A-Z,a-z,0-9
317 * use std::rand::RngUtil;
320 * let mut rng = rand::rng();
321 * println(rng.gen_str(8));
325 fn gen_str(&mut self, len: uint) -> ~str;
327 * Return a random byte string of the specified length
334 * use std::rand::RngUtil;
337 * let mut rng = rand::rng();
338 * printfln!(rng.gen_bytes(8));
342 fn gen_bytes(&mut self, len: uint) -> ~[u8];
344 * Choose an item randomly, failing if values is empty
351 * use std::rand::RngUtil;
354 * let mut rng = rand::rng();
355 * printfln!("%d", rng.choose([1,2,4,8,16,32]));
359 fn choose<T:Clone>(&mut self, values: &[T]) -> T;
360 /// Choose Some(item) randomly, returning None if values is empty
361 fn choose_option<T:Clone>(&mut self, values: &[T]) -> Option<T>;
363 * Choose an item respecting the relative weights, failing if the sum of
371 * use std::rand::RngUtil;
374 * let mut rng = rand::rng();
375 * let x = [rand::Weighted {weight: 4, item: 'a'},
376 * rand::Weighted {weight: 2, item: 'b'},
377 * rand::Weighted {weight: 2, item: 'c'}];
378 * printfln!("%c", rng.choose_weighted(x));
382 fn choose_weighted<T:Clone>(&mut self, v : &[Weighted<T>]) -> T;
384 * Choose Some(item) respecting the relative weights, returning none if
385 * the sum of the weights is 0
392 * use std::rand::RngUtil;
395 * let mut rng = rand::rng();
396 * let x = [rand::Weighted {weight: 4, item: 'a'},
397 * rand::Weighted {weight: 2, item: 'b'},
398 * rand::Weighted {weight: 2, item: 'c'}];
399 * printfln!(rng.choose_weighted_option(x));
403 fn choose_weighted_option<T:Clone>(&mut self, v: &[Weighted<T>])
406 * Return a vec containing copies of the items, in order, where
407 * the weight of the item determines how many copies there are
414 * use std::rand::RngUtil;
417 * let mut rng = rand::rng();
418 * let x = [rand::Weighted {weight: 4, item: 'a'},
419 * rand::Weighted {weight: 2, item: 'b'},
420 * rand::Weighted {weight: 2, item: 'c'}];
421 * printfln!(rng.weighted_vec(x));
425 fn weighted_vec<T:Clone>(&mut self, v: &[Weighted<T>]) -> ~[T];
434 * use std::rand::RngUtil;
437 * let mut rng = rand::rng();
438 * printfln!(rng.shuffle([1,2,3]));
442 fn shuffle<T:Clone>(&mut self, values: &[T]) -> ~[T];
444 * Shuffle a mutable vec in place
451 * use std::rand::RngUtil;
454 * let mut rng = rand::rng();
455 * let mut y = [1,2,3];
456 * rng.shuffle_mut(y);
458 * rng.shuffle_mut(y);
463 fn shuffle_mut<T>(&mut self, values: &mut [T]);
466 /// Extension methods for random number generators
467 impl<R: Rng> RngUtil for R {
468 /// Return a random value for a Rand type
470 fn gen<T: Rand>(&mut self) -> T {
475 * Return an int randomly chosen from the range [start, end),
476 * failing if start >= end
478 fn gen_int_range(&mut self, start: int, end: int) -> int {
479 assert!(start < end);
480 start + num::abs(self.gen::<int>() % (end - start))
484 * Return a uint randomly chosen from the range [start, end),
485 * failing if start >= end
487 fn gen_uint_range(&mut self, start: uint, end: uint) -> uint {
488 assert!(start < end);
489 start + (self.gen::<uint>() % (end - start))
493 * Return a char randomly chosen from chars, failing if chars is empty
495 fn gen_char_from(&mut self, chars: &str) -> char {
496 assert!(!chars.is_empty());
498 for c in chars.iter() { cs.push(c) }
502 /// Return a bool with a 1-in-n chance of true
503 fn gen_weighted_bool(&mut self, n: uint) -> bool {
507 self.gen_uint_range(1u, n + 1u) == 1u
512 * Return a random string of the specified length composed of A-Z,a-z,0-9
514 fn gen_str(&mut self, len: uint) -> ~str {
515 let charset = ~"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
516 abcdefghijklmnopqrstuvwxyz\
521 s = s + str::from_char(self.gen_char_from(charset));
527 /// Return a random byte string of the specified length
528 fn gen_bytes(&mut self, len: uint) -> ~[u8] {
529 do vec::from_fn(len) |_i| {
534 /// Choose an item randomly, failing if values is empty
535 fn choose<T:Clone>(&mut self, values: &[T]) -> T {
536 self.choose_option(values).unwrap()
539 /// Choose Some(item) randomly, returning None if values is empty
540 fn choose_option<T:Clone>(&mut self, values: &[T]) -> Option<T> {
541 if values.is_empty() {
544 Some(values[self.gen_uint_range(0u, values.len())].clone())
548 * Choose an item respecting the relative weights, failing if the sum of
551 fn choose_weighted<T:Clone>(&mut self, v: &[Weighted<T>]) -> T {
552 self.choose_weighted_option(v).unwrap()
556 * Choose Some(item) respecting the relative weights, returning none if
557 * the sum of the weights is 0
559 fn choose_weighted_option<T:Clone>(&mut self, v: &[Weighted<T>])
562 for item in v.iter() {
563 total += item.weight;
568 let chosen = self.gen_uint_range(0u, total);
570 for item in v.iter() {
571 so_far += item.weight;
573 return Some(item.item.clone());
580 * Return a vec containing copies of the items, in order, where
581 * the weight of the item determines how many copies there are
583 fn weighted_vec<T:Clone>(&mut self, v: &[Weighted<T>]) -> ~[T] {
585 for item in v.iter() {
586 for _ in range(0u, item.weight) {
587 r.push(item.item.clone());
594 fn shuffle<T:Clone>(&mut self, values: &[T]) -> ~[T] {
595 let mut m = values.to_owned();
600 /// Shuffle a mutable vec in place
601 fn shuffle_mut<T>(&mut self, values: &mut [T]) {
602 let mut i = values.len();
604 // invariant: elements with index >= i have been locked in place.
606 // lock element i in place.
607 values.swap(i, self.gen_uint_range(0u, i + 1u));
612 /// Create a random number generator with a default algorithm and seed.
614 /// It returns the cryptographically-safest `Rng` algorithm currently
615 /// available in Rust. If you require a specifically seeded `Rng` for
616 /// consistency over time you should pick one algorithm and create the
618 pub fn rng() -> IsaacRng {
622 static RAND_SIZE_LEN: u32 = 8;
623 static RAND_SIZE: u32 = 1 << RAND_SIZE_LEN;
625 /// A random number generator that uses the [ISAAC
626 /// algorithm](http://en.wikipedia.org/wiki/ISAAC_%28cipher%29).
628 /// The ISAAC algorithm is suitable for cryptographic purposes.
629 pub struct IsaacRng {
631 priv rsl: [u32, .. RAND_SIZE],
632 priv mem: [u32, .. RAND_SIZE],
639 /// Create an ISAAC random number generator with a random seed.
640 pub fn new() -> IsaacRng {
641 IsaacRng::new_seeded(seed())
644 /// Create an ISAAC random number generator with a seed. This can be any
645 /// length, although the maximum number of bytes used is 1024 and any more
646 /// will be silently ignored. A generator constructed with a given seed
647 /// will generate the same sequence of values as all other generators
648 /// constructed with the same seed.
649 pub fn new_seeded(seed: &[u8]) -> IsaacRng {
650 let mut rng = IsaacRng {
652 rsl: [0, .. RAND_SIZE],
653 mem: [0, .. RAND_SIZE],
657 let array_size = sys::size_of_val(&rng.rsl);
658 let copy_length = cmp::min(array_size, seed.len());
660 // manually create a &mut [u8] slice of randrsl to copy into.
661 let dest = unsafe { cast::transmute((&mut rng.rsl, array_size)) };
662 vec::bytes::copy_memory(dest, seed, copy_length);
667 /// Create an ISAAC random number generator using the default
669 pub fn new_unseeded() -> IsaacRng {
670 let mut rng = IsaacRng {
672 rsl: [0, .. RAND_SIZE],
673 mem: [0, .. RAND_SIZE],
680 /// Initialises `self`. If `use_rsl` is true, then use the current value
681 /// of `rsl` as a seed, otherwise construct one algorithmically (not
683 fn init(&mut self, use_rsl: bool) {
684 let mut a = 0x9e3779b9;
695 a^=b<<11; d+=a; b+=c;
698 d^=e>>16; g+=d; e+=f;
699 e^=f<<10; h+=e; f+=g;
706 do 4.times { mix!(); }
709 macro_rules! memloop (
711 do u32::range_step(0, RAND_SIZE, 8) |i| {
712 a+=$arr[i ]; b+=$arr[i+1];
713 c+=$arr[i+2]; d+=$arr[i+3];
714 e+=$arr[i+4]; f+=$arr[i+5];
715 g+=$arr[i+6]; h+=$arr[i+7];
717 self.mem[i ]=a; self.mem[i+1]=b;
718 self.mem[i+2]=c; self.mem[i+3]=d;
719 self.mem[i+4]=e; self.mem[i+5]=f;
720 self.mem[i+6]=g; self.mem[i+7]=h;
729 do u32::range_step(0, RAND_SIZE, 8) |i| {
731 self.mem[i ]=a; self.mem[i+1]=b;
732 self.mem[i+2]=c; self.mem[i+3]=d;
733 self.mem[i+4]=e; self.mem[i+5]=f;
734 self.mem[i+6]=g; self.mem[i+7]=h;
742 /// Refills the output buffer (`self.rsl`)
744 fn isaac(&mut self) {
748 let mut b = self.b + self.c;
750 static MIDPOINT: uint = RAND_SIZE as uint / 2;
752 macro_rules! ind (($x:expr) => {
753 self.mem[($x >> 2) & (RAND_SIZE - 1)]
755 macro_rules! rngstep(
756 ($j:expr, $shift:expr) => {{
757 let base = base + $j;
758 let mix = if $shift < 0 {
764 let x = self.mem[base + mr_offset];
765 a = (a ^ mix) + self.mem[base + m2_offset];
766 let y = ind!(x) + a + b;
767 self.mem[base + mr_offset] = y;
769 b = ind!(y >> RAND_SIZE_LEN) + x;
770 self.rsl[base + mr_offset] = b;
774 let r = [(0, MIDPOINT), (MIDPOINT, 0)];
775 for &(mr_offset, m2_offset) in r.iter() {
776 do uint::range_step(0, MIDPOINT, 4) |base| {
787 self.cnt = RAND_SIZE;
791 impl Rng for IsaacRng {
793 fn next(&mut self) -> u32 {
795 // make some more numbers
803 /// An [Xorshift random number
804 /// generator](http://en.wikipedia.org/wiki/Xorshift).
806 /// The Xorshift algorithm is not suitable for cryptographic purposes
807 /// but is very fast. If you do not know for sure that it fits your
808 /// requirements, use a more secure one such as `IsaacRng`.
809 pub struct XorShiftRng {
816 impl Rng for XorShiftRng {
818 pub fn next(&mut self) -> u32 {
820 let t = x ^ (x << 11);
825 self.w = w ^ (w >> 19) ^ (t ^ (t >> 8));
831 /// Create an xor shift random number generator with a default seed.
832 pub fn new() -> XorShiftRng {
833 // constants taken from http://en.wikipedia.org/wiki/Xorshift
834 XorShiftRng::new_seeded(123456789u32,
841 * Create a random number generator using the specified seed. A generator
842 * constructed with a given seed will generate the same sequence of values
843 * as all other generators constructed with the same seed.
845 pub fn new_seeded(x: u32, y: u32, z: u32, w: u32) -> XorShiftRng {
855 /// Create a new random seed.
856 pub fn seed() -> ~[u8] {
858 let n = rustrt::rand_seed_size() as uint;
859 let mut s = vec::from_elem(n, 0_u8);
860 do s.as_mut_buf |p, sz| {
861 rustrt::rand_gen_seed(p, sz as size_t)
867 // used to make space in TLS for a random number generator
868 static tls_rng_state: local_data::Key<@@mut IsaacRng> = &local_data::Key;
871 * Gives back a lazily initialized task-local random number generator,
872 * seeded by the system. Intended to be used in method chaining style, ie
873 * `task_rng().gen::<int>()`.
876 pub fn task_rng() -> @mut IsaacRng {
877 let r = local_data::get(tls_rng_state, |k| k.map(|&k| *k));
880 let rng = @@mut IsaacRng::new_seeded(seed());
881 local_data::set(tls_rng_state, rng);
888 // Allow direct chaining with `task_rng`
889 impl<R: Rng> Rng for @mut R {
891 fn next(&mut self) -> u32 {
897 * Returns a random value of a Rand type, using the task's random number
901 pub fn random<T: Rand>() -> T {
907 use option::{Option, Some};
911 fn test_rng_seeded() {
913 let mut ra = IsaacRng::new_seeded(seed);
914 let mut rb = IsaacRng::new_seeded(seed);
915 assert_eq!(ra.gen_str(100u), rb.gen_str(100u));
919 fn test_rng_seeded_custom_seed() {
920 // much shorter than generated seeds which are 1024 bytes
921 let seed = [2u8, 32u8, 4u8, 32u8, 51u8];
922 let mut ra = IsaacRng::new_seeded(seed);
923 let mut rb = IsaacRng::new_seeded(seed);
924 assert_eq!(ra.gen_str(100u), rb.gen_str(100u));
928 fn test_rng_seeded_custom_seed2() {
929 let seed = [2u8, 32u8, 4u8, 32u8, 51u8];
930 let mut ra = IsaacRng::new_seeded(seed);
931 // Regression test that isaac is actually using the above vector
934 assert!(r == 890007737u32 // on x86_64
935 || r == 2935188040u32); // on x86
939 fn test_gen_int_range() {
941 let a = r.gen_int_range(-3, 42);
942 assert!(a >= -3 && a < 42);
943 assert_eq!(r.gen_int_range(0, 1), 0);
944 assert_eq!(r.gen_int_range(-12, -11), -12);
949 #[ignore(cfg(windows))]
950 fn test_gen_int_from_fail() {
952 r.gen_int_range(5, -2);
956 fn test_gen_uint_range() {
958 let a = r.gen_uint_range(3u, 42u);
959 assert!(a >= 3u && a < 42u);
960 assert_eq!(r.gen_uint_range(0u, 1u), 0u);
961 assert_eq!(r.gen_uint_range(12u, 13u), 12u);
966 #[ignore(cfg(windows))]
967 fn test_gen_uint_range_fail() {
969 r.gen_uint_range(5u, 2u);
973 fn test_gen_float() {
975 let a = r.gen::<float>();
976 let b = r.gen::<float>();
981 fn test_gen_weighted_bool() {
983 assert_eq!(r.gen_weighted_bool(0u), true);
984 assert_eq!(r.gen_weighted_bool(1u), true);
990 debug!(r.gen_str(10u));
991 debug!(r.gen_str(10u));
992 debug!(r.gen_str(10u));
993 assert_eq!(r.gen_str(0u).len(), 0u);
994 assert_eq!(r.gen_str(10u).len(), 10u);
995 assert_eq!(r.gen_str(16u).len(), 16u);
999 fn test_gen_bytes() {
1001 assert_eq!(r.gen_bytes(0u).len(), 0u);
1002 assert_eq!(r.gen_bytes(10u).len(), 10u);
1003 assert_eq!(r.gen_bytes(16u).len(), 16u);
1009 assert_eq!(r.choose([1, 1, 1]), 1);
1013 fn test_choose_option() {
1015 let x: Option<int> = r.choose_option([]);
1016 assert!(x.is_none());
1017 assert_eq!(r.choose_option([1, 1, 1]), Some(1));
1021 fn test_choose_weighted() {
1023 assert!(r.choose_weighted([
1024 Weighted { weight: 1u, item: 42 },
1026 assert!(r.choose_weighted([
1027 Weighted { weight: 0u, item: 42 },
1028 Weighted { weight: 1u, item: 43 },
1033 fn test_choose_weighted_option() {
1035 assert!(r.choose_weighted_option([
1036 Weighted { weight: 1u, item: 42 },
1038 assert!(r.choose_weighted_option([
1039 Weighted { weight: 0u, item: 42 },
1040 Weighted { weight: 1u, item: 43 },
1042 let v: Option<int> = r.choose_weighted_option([]);
1043 assert!(v.is_none());
1047 fn test_weighted_vec() {
1049 let empty: ~[int] = ~[];
1050 assert_eq!(r.weighted_vec([]), empty);
1051 assert!(r.weighted_vec([
1052 Weighted { weight: 0u, item: 3u },
1053 Weighted { weight: 1u, item: 2u },
1054 Weighted { weight: 2u, item: 1u },
1055 ]) == ~[2u, 1u, 1u]);
1061 let empty: ~[int] = ~[];
1062 assert_eq!(r.shuffle([]), empty);
1063 assert_eq!(r.shuffle([1, 1, 1]), ~[1, 1, 1]);
1067 fn test_task_rng() {
1068 let mut r = task_rng();
1070 assert_eq!(r.shuffle([1, 1, 1]), ~[1, 1, 1]);
1071 assert_eq!(r.gen_uint_range(0u, 1u), 0u);
1076 // not sure how to test this aside from just getting some values
1077 let _n : uint = random();
1078 let _f : f32 = random();
1079 let _o : Option<Option<i8>> = random();
1081 (~uint, @int, ~Option<~(@char, ~(@bool,))>),
1082 (u8, i8, u16, i16, u32, i32, u64, i64),
1083 (f32, (f64, (float,)))) = random();
1087 fn compare_isaac_implementation() {
1088 // This is to verify that the implementation of the ISAAC rng is
1089 // correct (i.e. matches the output of the upstream implementation,
1090 // which is in the runtime)
1097 #[allow(non_camel_case_types)] // runtime type
1098 pub enum rust_rng {}
1101 pub fn rand_new_seeded(buf: *u8, sz: size_t) -> *rust_rng;
1102 pub fn rand_next(rng: *rust_rng) -> u32;
1103 pub fn rand_free(rng: *rust_rng);
1107 // run against several seeds
1110 let seed = super::seed();
1111 let rt_rng = do seed.as_imm_buf |p, sz| {
1112 rustrt::rand_new_seeded(p, sz as size_t)
1114 let mut rng = IsaacRng::new_seeded(seed);
1117 assert_eq!(rng.next(), rustrt::rand_next(rt_rng));
1119 rustrt::rand_free(rt_rng);
1127 use extra::test::BenchHarness;
1132 fn rand_xorshift(bh: &mut BenchHarness) {
1133 let mut rng = XorShiftRng::new();
1137 bh.bytes = size_of::<uint>() as u64;
1141 fn rand_isaac(bh: &mut BenchHarness) {
1142 let mut rng = IsaacRng::new();
1146 bh.bytes = size_of::<uint>() as u64;
1150 fn rand_shuffle_100(bh: &mut BenchHarness) {
1151 let mut rng = XorShiftRng::new();
1152 let x : &mut[uint] = [1,..100];