Skip to content

Commit cf34b31

Browse files
committed
auto merge of #5825 : danluu/rust/rngdoc, r=erickt
This adds an example for most of the methods in Rng. As a total newcomer to Rust, it took a while to figure out how to do basic things like use library functions, because there aren't many usage examples, and most examples that Google turns up are out of date. Something like this would have saved me a bit of time. This might be a bit verbose. Some alternative options would be to consolidate all the examples into one section, or to only have code for the specific function call inline.
2 parents a3c8f52 + aff5589 commit cf34b31

File tree

1 file changed

+183
-9
lines changed

1 file changed

+183
-9
lines changed

src/libcore/rand.rs

+183-9
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,21 @@ pub struct Weighted<T> {
150150

151151
pub trait RngUtil {
152152
fn gen<T:Rand>(&self) -> T;
153-
/// Return a random int
153+
/**
154+
* Return a random int
155+
*
156+
* *Example*
157+
*
158+
* ~~~
159+
*
160+
* use core::rand::RngUtil;
161+
*
162+
* fn main() {
163+
* rng = rand::Rng();
164+
* println(fmt!("%d",rng.gen_int()));
165+
* }
166+
* ~~~
167+
*/
154168
fn gen_int(&self) -> int;
155169
fn gen_int_range(&self, start: int, end: int) -> int;
156170
/// Return a random i8
@@ -176,7 +190,21 @@ pub trait RngUtil {
176190
fn gen_u32(&self) -> u32;
177191
/// Return a random u64
178192
fn gen_u64(&self) -> u64;
179-
/// Return a random float in the interval [0,1]
193+
/**
194+
* Return random float in the interval [0,1]
195+
*
196+
* *Example*
197+
*
198+
* ~~~
199+
*
200+
* use core::rand::RngUtil;
201+
*
202+
* fn main() {
203+
* rng = rand::Rng();
204+
* println(fmt!("%f",rng.gen_float()));
205+
* }
206+
* ~~~
207+
*/
180208
fn gen_float(&self) -> float;
181209
/// Return a random f32 in the interval [0,1]
182210
fn gen_f32(&self) -> f32;
@@ -188,38 +216,184 @@ pub trait RngUtil {
188216
* Return a char randomly chosen from chars, failing if chars is empty
189217
*/
190218
fn gen_char_from(&self, chars: &str) -> char;
191-
/// Return a random bool
219+
/**
220+
* Return a random bool
221+
*
222+
* *Example*
223+
*
224+
* ~~~
225+
*
226+
* use core::rand::RngUtil;
227+
*
228+
* fn main() {
229+
* rng = rand::Rng();
230+
* println(fmt!("%b",rng.gen_bool()));
231+
* }
232+
* ~~~
233+
*/
192234
fn gen_bool(&self) -> bool;
193-
/// Return a bool with a 1 in n chance of true
235+
/**
236+
* Return a bool with a 1 in n chance of true
237+
*
238+
* *Example*
239+
*
240+
* ~~~
241+
*
242+
* use core::rand::RngUtil;
243+
*
244+
* fn main() {
245+
* rng = rand::Rng();
246+
* println(fmt!("%b",rng.gen_weighted_bool(3)));
247+
* }
248+
* ~~~
249+
*/
194250
fn gen_weighted_bool(&self, n: uint) -> bool;
195251
/**
196252
* Return a random string of the specified length composed of A-Z,a-z,0-9
253+
*
254+
* *Example*
255+
*
256+
* ~~~
257+
*
258+
* use core::rand::RngUtil;
259+
*
260+
* fn main() {
261+
* rng = rand::Rng();
262+
* println(rng.gen_str(8));
263+
* }
264+
* ~~~
197265
*/
198266
fn gen_str(&self, len: uint) -> ~str;
199-
/// Return a random byte string of the specified length
267+
/**
268+
* Return a random byte string of the specified length
269+
*
270+
* *Example*
271+
*
272+
* ~~~
273+
*
274+
* use core::rand::RngUtil;
275+
*
276+
* fn main() {
277+
* rng = rand::Rng();
278+
* println(fmt!("%?",rng.gen_bytes(8)));
279+
* }
280+
* ~~~
281+
*/
200282
fn gen_bytes(&self, len: uint) -> ~[u8];
201-
/// Choose an item randomly, failing if values is empty
283+
///
284+
/**
285+
* Choose an item randomly, failing if values is empty
286+
*
287+
* *Example*
288+
*
289+
* ~~~
290+
*
291+
* use core::rand::RngUtil;
292+
*
293+
* fn main() {
294+
* rng = rand::Rng();
295+
* println(fmt!("%d",rng.choose([1,2,4,8,16,32])));
296+
* }
297+
* ~~~
298+
*/
202299
fn choose<T:Copy>(&self, values: &[T]) -> T;
203300
/// Choose Some(item) randomly, returning None if values is empty
204301
fn choose_option<T:Copy>(&self, values: &[T]) -> Option<T>;
205302
/**
206303
* Choose an item respecting the relative weights, failing if the sum of
207304
* the weights is 0
305+
*
306+
* *Example*
307+
*
308+
* ~~~
309+
*
310+
* use core::rand::RngUtil;
311+
*
312+
* fn main() {
313+
* rng = rand::Rng();
314+
* let x = [rand::Weighted {weight: 4, item: 'a'},
315+
* rand::Weighted {weight: 2, item: 'b'},
316+
* rand::Weighted {weight: 2, item: 'c'}];
317+
* println(fmt!("%c",rng.choose_weighted(x)));
318+
* }
319+
* ~~~
208320
*/
209321
fn choose_weighted<T:Copy>(&self, v : &[Weighted<T>]) -> T;
210322
/**
211323
* Choose Some(item) respecting the relative weights, returning none if
212324
* the sum of the weights is 0
325+
*
326+
* *Example*
327+
*
328+
* ~~~
329+
*
330+
* use core::rand::RngUtil;
331+
*
332+
* fn main() {
333+
* rng = rand::Rng();
334+
* let x = [rand::Weighted {weight: 4, item: 'a'},
335+
* rand::Weighted {weight: 2, item: 'b'},
336+
* rand::Weighted {weight: 2, item: 'c'}];
337+
* println(fmt!("%?",rng.choose_weighted_option(x)));
338+
* }
339+
* ~~~
213340
*/
214341
fn choose_weighted_option<T:Copy>(&self, v: &[Weighted<T>]) -> Option<T>;
215342
/**
216343
* Return a vec containing copies of the items, in order, where
217344
* the weight of the item determines how many copies there are
345+
*
346+
* *Example*
347+
*
348+
* ~~~
349+
*
350+
* use core::rand::RngUtil;
351+
*
352+
* fn main() {
353+
* rng = rand::Rng();
354+
* let x = [rand::Weighted {weight: 4, item: 'a'},
355+
* rand::Weighted {weight: 2, item: 'b'},
356+
* rand::Weighted {weight: 2, item: 'c'}];
357+
* println(fmt!("%?",rng.weighted_vec(x)));
358+
* }
359+
* ~~~
218360
*/
219361
fn weighted_vec<T:Copy>(&self, v: &[Weighted<T>]) -> ~[T];
220-
/// Shuffle a vec
362+
/**
363+
* Shuffle a vec
364+
*
365+
* *Example*
366+
*
367+
* ~~~
368+
*
369+
* use core::rand::RngUtil;
370+
*
371+
* fn main() {
372+
* rng = rand::Rng();
373+
* println(fmt!("%?",rng.shuffle([1,2,3])));
374+
* }
375+
* ~~~
376+
*/
221377
fn shuffle<T:Copy>(&self, values: &[T]) -> ~[T];
222-
/// Shuffle a mutable vec in place
378+
/**
379+
* Shuffle a mutable vec in place
380+
*
381+
* *Example*
382+
*
383+
* ~~~
384+
*
385+
* use core::rand::RngUtil;
386+
*
387+
* fn main() {
388+
* rng = rand::Rng();
389+
* let mut y = [1,2,3];
390+
* rng.shuffle_mut(y);
391+
* println(fmt!("%?",y));
392+
* rng.shuffle_mut(y);
393+
* println(fmt!("%?",y));
394+
* }
395+
* ~~~
396+
*/
223397
fn shuffle_mut<T>(&self, values: &mut [T]);
224398
}
225399

@@ -337,7 +511,7 @@ impl RngUtil for @Rng {
337511
self.next() & 1u32 == 1u32
338512
}
339513

340-
/// Return a bool with a 1 in n chance of true
514+
/// Return a bool with a 1-in-n chance of true
341515
fn gen_weighted_bool(&self, n: uint) -> bool {
342516
if n == 0u {
343517
true

0 commit comments

Comments
 (0)