@@ -341,88 +341,8 @@ pub trait Rand : Sized {
341
341
///
342
342
/// [`RngCore`]: https://docs.rs/rand_core/0.1/rand_core/trait.RngCore.html
343
343
pub trait Rng : RngCore {
344
- /// Fill `dest` entirely with random bytes (uniform value distribution),
345
- /// where `dest` is any type supporting [`AsByteSliceMut`], namely slices
346
- /// and arrays over primitive integer types (`i8`, `i16`, `u32`, etc.).
347
- ///
348
- /// On big-endian platforms this performs byte-swapping to ensure
349
- /// portability of results from reproducible generators.
350
- ///
351
- /// This uses [`fill_bytes`] internally which may handle some RNG errors
352
- /// implicitly (e.g. waiting if the OS generator is not ready), but panics
353
- /// on other errors. See also [`try_fill`] which returns errors.
354
- ///
355
- /// # Example
356
- ///
357
- /// ```rust
358
- /// use rand::{thread_rng, Rng};
359
- ///
360
- /// let mut arr = [0i8; 20];
361
- /// thread_rng().fill(&mut arr[..]);
362
- /// ```
363
- ///
364
- /// [`fill_bytes`]: https://docs.rs/rand_core/0.1/rand_core/trait.RngCore.html#method.fill_bytes
365
- /// [`try_fill`]: trait.Rng.html#method.try_fill
366
- /// [`AsByteSliceMut`]: trait.AsByteSliceMut.html
367
- fn fill < T : AsByteSliceMut + ?Sized > ( & mut self , dest : & mut T ) {
368
- self . fill_bytes ( dest. as_byte_slice_mut ( ) ) ;
369
- dest. to_le ( ) ;
370
- }
371
-
372
- /// Fill `dest` entirely with random bytes (uniform value distribution),
373
- /// where `dest` is any type supporting [`AsByteSliceMut`], namely slices
374
- /// and arrays over primitive integer types (`i8`, `i16`, `u32`, etc.).
375
- ///
376
- /// On big-endian platforms this performs byte-swapping to ensure
377
- /// portability of results from reproducible generators.
378
- ///
379
- /// This uses [`try_fill_bytes`] internally and forwards all RNG errors. In
380
- /// some cases errors may be resolvable; see [`ErrorKind`] and
381
- /// documentation for the RNG in use. If you do not plan to handle these
382
- /// errors you may prefer to use [`fill`].
383
- ///
384
- /// # Example
385
- ///
386
- /// ```rust
387
- /// # use rand::Error;
388
- /// use rand::{thread_rng, Rng};
389
- ///
390
- /// # fn try_inner() -> Result<(), Error> {
391
- /// let mut arr = [0u64; 4];
392
- /// thread_rng().try_fill(&mut arr[..])?;
393
- /// # Ok(())
394
- /// # }
395
- ///
396
- /// # try_inner().unwrap()
397
- /// ```
398
- ///
399
- /// [`ErrorKind`]: https://docs.rs/rand_core/0.1/rand_core/enum.ErrorKind.html
400
- /// [`try_fill_bytes`]: https://docs.rs/rand_core/0.1/rand_core/trait.RngCore.html#method.try_fill_bytes
401
- /// [`fill`]: trait.Rng.html#method.fill
402
- /// [`AsByteSliceMut`]: trait.AsByteSliceMut.html
403
- fn try_fill < T : AsByteSliceMut + ?Sized > ( & mut self , dest : & mut T ) -> Result < ( ) , Error > {
404
- self . try_fill_bytes ( dest. as_byte_slice_mut ( ) ) ?;
405
- dest. to_le ( ) ;
406
- Ok ( ( ) )
407
- }
408
-
409
- /// Sample a new value, using the given distribution.
410
- ///
411
- /// ### Example
412
- ///
413
- /// ```rust
414
- /// use rand::{thread_rng, Rng};
415
- /// use rand::distributions::Range;
416
- ///
417
- /// let mut rng = thread_rng();
418
- /// let x: i32 = rng.sample(Range::new(10, 15));
419
- /// ```
420
- fn sample < T , D : Distribution < T > > ( & mut self , distr : D ) -> T {
421
- distr. sample ( self )
422
- }
423
-
424
344
/// Return a random value supporting the [`Uniform`] distribution.
425
- ///
345
+ ///
426
346
/// [`Uniform`]: struct.Uniform.html
427
347
///
428
348
/// # Example
@@ -440,27 +360,6 @@ pub trait Rng: RngCore {
440
360
Uniform . sample ( self )
441
361
}
442
362
443
- /// Return an iterator that will yield an infinite number of randomly
444
- /// generated items.
445
- ///
446
- /// # Example
447
- ///
448
- /// ```
449
- /// # #![allow(deprecated)]
450
- /// use rand::{thread_rng, Rng};
451
- ///
452
- /// let mut rng = thread_rng();
453
- /// let x = rng.gen_iter::<u32>().take(10).collect::<Vec<u32>>();
454
- /// println!("{:?}", x);
455
- /// println!("{:?}", rng.gen_iter::<(f64, bool)>().take(5)
456
- /// .collect::<Vec<(f64, bool)>>());
457
- /// ```
458
- #[ allow( deprecated) ]
459
- #[ deprecated( since="0.5.0" , note="use iter::repeat instead" ) ]
460
- fn gen_iter < T > ( & mut self ) -> Generator < T , & mut Self > where Uniform : Distribution < T > {
461
- Generator { rng : self , _marker : marker:: PhantomData }
462
- }
463
-
464
363
/// Generate a random value in the range [`low`, `high`), i.e. inclusive of
465
364
/// `low` and exclusive of `high`.
466
365
///
@@ -489,61 +388,101 @@ pub trait Rng: RngCore {
489
388
Range :: sample_single ( low, high, self )
490
389
}
491
390
492
- /// Return a bool with a 1 in n chance of true
391
+ /// Sample a new value, using the given distribution.
493
392
///
494
- /// # Example
393
+ /// ### Example
495
394
///
496
395
/// ```rust
497
- /// # #![allow(deprecated)]
498
396
/// use rand::{thread_rng, Rng};
397
+ /// use rand::distributions::Range;
499
398
///
500
399
/// let mut rng = thread_rng();
501
- /// assert_eq!(rng.gen_weighted_bool(0), true);
502
- /// assert_eq!(rng.gen_weighted_bool(1), true);
503
- /// // Just like `rng.gen::<bool>()` a 50-50% chance, but using a slower
504
- /// // method with different results.
505
- /// println!("{}", rng.gen_weighted_bool(2));
506
- /// // First meaningful use of `gen_weighted_bool`.
507
- /// println!("{}", rng.gen_weighted_bool(3));
400
+ /// let x: i32 = rng.sample(Range::new(10, 15));
508
401
/// ```
509
- #[ deprecated( since="0.5.0" , note="use gen_bool instead" ) ]
510
- fn gen_weighted_bool ( & mut self , n : u32 ) -> bool {
511
- // Short-circuit after `n <= 1` to avoid panic in `gen_range`
512
- n <= 1 || self . gen_range ( 0 , n) == 0
402
+ fn sample < T , D : Distribution < T > > ( & mut self , distr : D ) -> T {
403
+ distr. sample ( self )
513
404
}
514
405
515
- /// Return a bool with a probability `p` of being true.
406
+ /// Fill `dest` entirely with random bytes (uniform value distribution),
407
+ /// where `dest` is any type supporting [`AsByteSliceMut`], namely slices
408
+ /// and arrays over primitive integer types (`i8`, `i16`, `u32`, etc.).
409
+ ///
410
+ /// On big-endian platforms this performs byte-swapping to ensure
411
+ /// portability of results from reproducible generators.
412
+ ///
413
+ /// This uses [`fill_bytes`] internally which may handle some RNG errors
414
+ /// implicitly (e.g. waiting if the OS generator is not ready), but panics
415
+ /// on other errors. See also [`try_fill`] which returns errors.
516
416
///
517
417
/// # Example
518
418
///
519
419
/// ```rust
520
420
/// use rand::{thread_rng, Rng};
521
421
///
522
- /// let mut rng = thread_rng() ;
523
- /// println!("{}", rng.gen_bool(1.0 / 3.0) );
422
+ /// let mut arr = [0i8; 20] ;
423
+ /// thread_rng().fill(&mut arr[..] );
524
424
/// ```
525
- fn gen_bool ( & mut self , p : f64 ) -> bool {
526
- assert ! ( p >= 0.0 && p <= 1.0 ) ;
527
- // If `p` is constant, this will be evaluated at compile-time.
528
- let p_int = ( p * f64:: from ( core:: u32:: MAX ) ) as u32 ;
529
- self . gen :: < u32 > ( ) <= p_int
425
+ ///
426
+ /// [`fill_bytes`]: https://docs.rs/rand_core/0.1/rand_core/trait.RngCore.html#method.fill_bytes
427
+ /// [`try_fill`]: trait.Rng.html#method.try_fill
428
+ /// [`AsByteSliceMut`]: trait.AsByteSliceMut.html
429
+ fn fill < T : AsByteSliceMut + ?Sized > ( & mut self , dest : & mut T ) {
430
+ self . fill_bytes ( dest. as_byte_slice_mut ( ) ) ;
431
+ dest. to_le ( ) ;
530
432
}
531
433
532
- /// Return an iterator of random characters from the set A-Z,a-z,0-9.
434
+ /// Fill `dest` entirely with random bytes (uniform value distribution),
435
+ /// where `dest` is any type supporting [`AsByteSliceMut`], namely slices
436
+ /// and arrays over primitive integer types (`i8`, `i16`, `u32`, etc.).
437
+ ///
438
+ /// On big-endian platforms this performs byte-swapping to ensure
439
+ /// portability of results from reproducible generators.
440
+ ///
441
+ /// This uses [`try_fill_bytes`] internally and forwards all RNG errors. In
442
+ /// some cases errors may be resolvable; see [`ErrorKind`] and
443
+ /// documentation for the RNG in use. If you do not plan to handle these
444
+ /// errors you may prefer to use [`fill`].
533
445
///
534
446
/// # Example
535
447
///
536
448
/// ```rust
537
- /// # #![allow(deprecated)]
449
+ /// # use rand::Error;
538
450
/// use rand::{thread_rng, Rng};
539
451
///
540
- /// let s: String = thread_rng().gen_ascii_chars().take(10).collect();
541
- /// println!("{}", s);
452
+ /// # fn try_inner() -> Result<(), Error> {
453
+ /// let mut arr = [0u64; 4];
454
+ /// thread_rng().try_fill(&mut arr[..])?;
455
+ /// # Ok(())
456
+ /// # }
457
+ ///
458
+ /// # try_inner().unwrap()
542
459
/// ```
543
- #[ allow( deprecated) ]
544
- #[ deprecated( since="0.5.0" , note="use distributions::Alphanumeric instead" ) ]
545
- fn gen_ascii_chars ( & mut self ) -> AsciiGenerator < & mut Self > {
546
- AsciiGenerator { rng : self }
460
+ ///
461
+ /// [`ErrorKind`]: https://docs.rs/rand_core/0.1/rand_core/enum.ErrorKind.html
462
+ /// [`try_fill_bytes`]: https://docs.rs/rand_core/0.1/rand_core/trait.RngCore.html#method.try_fill_bytes
463
+ /// [`fill`]: trait.Rng.html#method.fill
464
+ /// [`AsByteSliceMut`]: trait.AsByteSliceMut.html
465
+ fn try_fill < T : AsByteSliceMut + ?Sized > ( & mut self , dest : & mut T ) -> Result < ( ) , Error > {
466
+ self . try_fill_bytes ( dest. as_byte_slice_mut ( ) ) ?;
467
+ dest. to_le ( ) ;
468
+ Ok ( ( ) )
469
+ }
470
+
471
+ /// Return a bool with a probability `p` of being true.
472
+ ///
473
+ /// # Example
474
+ ///
475
+ /// ```rust
476
+ /// use rand::{thread_rng, Rng};
477
+ ///
478
+ /// let mut rng = thread_rng();
479
+ /// println!("{}", rng.gen_bool(1.0 / 3.0));
480
+ /// ```
481
+ fn gen_bool ( & mut self , p : f64 ) -> bool {
482
+ assert ! ( p >= 0.0 && p <= 1.0 ) ;
483
+ // If `p` is constant, this will be evaluated at compile-time.
484
+ let p_int = ( p * f64:: from ( core:: u32:: MAX ) ) as u32 ;
485
+ self . gen :: < u32 > ( ) <= p_int
547
486
}
548
487
549
488
/// Return a random element from `values`.
@@ -606,6 +545,67 @@ pub trait Rng: RngCore {
606
545
values. swap ( i, self . gen_range ( 0 , i + 1 ) ) ;
607
546
}
608
547
}
548
+
549
+ /// Return an iterator that will yield an infinite number of randomly
550
+ /// generated items.
551
+ ///
552
+ /// # Example
553
+ ///
554
+ /// ```
555
+ /// # #![allow(deprecated)]
556
+ /// use rand::{thread_rng, Rng};
557
+ ///
558
+ /// let mut rng = thread_rng();
559
+ /// let x = rng.gen_iter::<u32>().take(10).collect::<Vec<u32>>();
560
+ /// println!("{:?}", x);
561
+ /// println!("{:?}", rng.gen_iter::<(f64, bool)>().take(5)
562
+ /// .collect::<Vec<(f64, bool)>>());
563
+ /// ```
564
+ #[ allow( deprecated) ]
565
+ #[ deprecated( since="0.5.0" , note="use iter::repeat instead" ) ]
566
+ fn gen_iter < T > ( & mut self ) -> Generator < T , & mut Self > where Uniform : Distribution < T > {
567
+ Generator { rng : self , _marker : marker:: PhantomData }
568
+ }
569
+
570
+ /// Return a bool with a 1 in n chance of true
571
+ ///
572
+ /// # Example
573
+ ///
574
+ /// ```rust
575
+ /// # #![allow(deprecated)]
576
+ /// use rand::{thread_rng, Rng};
577
+ ///
578
+ /// let mut rng = thread_rng();
579
+ /// assert_eq!(rng.gen_weighted_bool(0), true);
580
+ /// assert_eq!(rng.gen_weighted_bool(1), true);
581
+ /// // Just like `rng.gen::<bool>()` a 50-50% chance, but using a slower
582
+ /// // method with different results.
583
+ /// println!("{}", rng.gen_weighted_bool(2));
584
+ /// // First meaningful use of `gen_weighted_bool`.
585
+ /// println!("{}", rng.gen_weighted_bool(3));
586
+ /// ```
587
+ #[ deprecated( since="0.5.0" , note="use gen_bool instead" ) ]
588
+ fn gen_weighted_bool ( & mut self , n : u32 ) -> bool {
589
+ // Short-circuit after `n <= 1` to avoid panic in `gen_range`
590
+ n <= 1 || self . gen_range ( 0 , n) == 0
591
+ }
592
+
593
+ /// Return an iterator of random characters from the set A-Z,a-z,0-9.
594
+ ///
595
+ /// # Example
596
+ ///
597
+ /// ```rust
598
+ /// # #![allow(deprecated)]
599
+ /// use rand::{thread_rng, Rng};
600
+ ///
601
+ /// let s: String = thread_rng().gen_ascii_chars().take(10).collect();
602
+ /// println!("{}", s);
603
+ /// ```
604
+ #[ allow( deprecated) ]
605
+ #[ deprecated( since="0.5.0" , note="use distributions::Alphanumeric instead" ) ]
606
+ fn gen_ascii_chars ( & mut self ) -> AsciiGenerator < & mut Self > {
607
+ AsciiGenerator { rng : self }
608
+ }
609
609
}
610
610
611
611
impl < R : RngCore + ?Sized > Rng for R { }
0 commit comments