]> git.proxmox.com Git - rustc.git/blob - src/vendor/rand-0.3.22/README.md
New upstream version 1.27.1+dfsg1
[rustc.git] / src / vendor / rand-0.3.22 / README.md
1 rand
2 ====
3
4 A Rust library for random number generators and other randomness functionality.
5
6 [![Build Status](https://travis-ci.org/rust-lang-nursery/rand.svg?branch=master)](https://travis-ci.org/rust-lang-nursery/rand)
7 [![Build status](https://ci.appveyor.com/api/projects/status/rm5c9o33k3jhchbw?svg=true)](https://ci.appveyor.com/project/alexcrichton/rand)
8
9 [Documentation](https://docs.rs/rand)
10
11 ## Compatibility upgrade
12
13 Version 0.3 has been replaced by a compatibility wrapper around `rand` 0.4. It
14 is recommended to update to 0.4.
15
16 ## Usage
17
18 Add this to your `Cargo.toml`:
19
20 ```toml
21 [dependencies]
22 rand = "0.4"
23 ```
24
25 and this to your crate root:
26
27 ```rust
28 extern crate rand;
29 ```
30
31 ## Examples
32
33 There is built-in support for a random number generator (RNG) associated with each thread stored in thread-local storage. This RNG can be accessed via thread_rng, or used implicitly via random. This RNG is normally randomly seeded from an operating-system source of randomness, e.g. /dev/urandom on Unix systems, and will automatically reseed itself from this source after generating 32 KiB of random data.
34
35 ```rust
36 let tuple = rand::random::<(f64, char)>();
37 println!("{:?}", tuple)
38 ```
39
40 ```rust
41 use rand::Rng;
42
43 let mut rng = rand::thread_rng();
44 if rng.gen() { // random bool
45 println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>())
46 }
47 ```
48
49 It is also possible to use other RNG types, which have a similar interface. The following uses the "ChaCha" algorithm instead of the default.
50
51 ```rust
52 use rand::{Rng, ChaChaRng};
53
54 let mut rng = rand::ChaChaRng::new_unseeded();
55 println!("i32: {}, u32: {}", rng.gen::<i32>(), rng.gen::<u32>())
56 ```
57
58 # `derive(Rand)`
59
60 You can derive the `Rand` trait for your custom type via the `#[derive(Rand)]`
61 directive. To use this first add this to your Cargo.toml:
62
63 ```toml
64 rand = "0.4"
65 rand_derive = "0.3"
66 ```
67
68 Next in your crate:
69
70 ```rust
71 extern crate rand;
72 #[macro_use]
73 extern crate rand_derive;
74
75 #[derive(Rand, Debug)]
76 struct MyStruct {
77 a: i32,
78 b: u32,
79 }
80
81 fn main() {
82 println!("{:?}", rand::random::<MyStruct>());
83 }
84 ```
85
86
87 # License
88
89 `rand` is primarily distributed under the terms of both the MIT
90 license and the Apache License (Version 2.0).
91
92 See LICENSE-APACHE, and LICENSE-MIT for details.