Skip to content

Commit 4b7d363

Browse files
committed
Add examples for Rng functions.
1 parent 2c64983 commit 4b7d363

File tree

1 file changed

+176
-8
lines changed

1 file changed

+176
-8
lines changed

src/libcore/rand.rs

+176-8
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,178 @@ 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'}, rand::Weighted {weight: 2, item: 'b'}, rand::Weighted {weight: 2, item: 'c'}];
315+
* println(fmt!("%c",rng.choose_weighted(x)));
316+
* }
317+
* ~~~
208318
*/
209319
fn choose_weighted<T:Copy>(&self, v : &[Weighted<T>]) -> T;
210320
/**
211321
* Choose Some(item) respecting the relative weights, returning none if
212322
* the sum of the weights is 0
323+
*
324+
* *Example*
325+
*
326+
* ~~~
327+
*
328+
* use core::rand::RngUtil;
329+
*
330+
* fn main() {
331+
* rng = rand::Rng();
332+
* let x = [rand::Weighted {weight: 4, item: 'a'}, rand::Weighted {weight: 2, item: 'b'}, rand::Weighted {weight: 2, item: 'c'}];
333+
* println(fmt!("%?",rng.choose_weighted_option(x)));
334+
* }
335+
* ~~~
213336
*/
214337
fn choose_weighted_option<T:Copy>(&self, v: &[Weighted<T>]) -> Option<T>;
215338
/**
216339
* Return a vec containing copies of the items, in order, where
217340
* the weight of the item determines how many copies there are
341+
*
342+
* *Example*
343+
*
344+
* ~~~
345+
*
346+
* use core::rand::RngUtil;
347+
*
348+
* fn main() {
349+
* rng = rand::Rng();
350+
* let x = [rand::Weighted {weight: 4, item: 'a'}, rand::Weighted {weight: 2, item: 'b'}, rand::Weighted {weight: 2, item: 'c'}];
351+
* println(fmt!("%?",rng.weighted_vec(x)));
352+
* }
353+
* ~~~
218354
*/
219355
fn weighted_vec<T:Copy>(&self, v: &[Weighted<T>]) -> ~[T];
220-
/// Shuffle a vec
356+
/**
357+
* Shuffle a vec
358+
*
359+
* *Example*
360+
*
361+
* ~~~
362+
*
363+
* use core::rand::RngUtil;
364+
*
365+
* fn main() {
366+
* rng = rand::Rng();
367+
* println(fmt!("%?",rng.shuffle([1,2,3])));
368+
* }
369+
* ~~~
370+
*/
221371
fn shuffle<T:Copy>(&self, values: &[T]) -> ~[T];
222-
/// Shuffle a mutable vec in place
372+
/**
373+
* Shuffle a mutable vec in place
374+
*
375+
* *Example*
376+
*
377+
* ~~~
378+
*
379+
* use core::rand::RngUtil;
380+
*
381+
* fn main() {
382+
* rng = rand::Rng();
383+
* let mut y = [1,2,3];
384+
* rng.shuffle_mut(y);
385+
* println(fmt!("%?",y));
386+
* rng.shuffle_mut(y);
387+
* println(fmt!("%?",y));
388+
* }
389+
* ~~~
390+
*/
223391
fn shuffle_mut<T>(&self, values: &mut [T]);
224392
}
225393

0 commit comments

Comments
 (0)