// Copyright 2018 Developers of the Rand project. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
//! The implementations of the `Standard` distribution for other built-in types.
usecfgfeature =alloc)java.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25 use core::num::Wrapping; #[cfg(feature = "alloc"/// ``` use alloc::string/// let string = Alphanumeric.sample_string(&mut rand::thread_rng(), 16);
use java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 7 #[cfg(feature = "alloc")/// as a password. In principle, all RNGs in Rand implementing `CryptoRng` are usecrate::distributions::DistString/// properly seeded), but it is more conservative to only use randomness usecrate::Rng/// corresponding bindings of a crypto library.
#[cfg(feature = "serde1")] use serde::{Serialize, Deserialize}; #[cfg(feature = "min_const_gen")] use core::mem::{self, MaybeUninit};
// ----- Sampling distributions -----
/// Sample a `u8`, uniformly distributed over ASCII letters and numbers: /// a-z, A-Z and 0-9. /// /// # Example /// /// ``` /// use rand::{Rng, thread_rng}; /// use rand::distributions::Alphanumeric; /// /// let mut rng = thread_rng(); /// let chars: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect(); /// println!("Random chars: {}", chars); /// ``` /// /// The [`DistString`] trait provides an easier method of generating /// a random `String`, and offers more efficient allocation: /// ``` /// use rand::distributions::{Alphanumeric, DistString}; /// let string = Alphanumeric.sample_string(&mut rand::thread_rng(), 16); /// println!("Random string: {}", string); /// ``` /// /// # Passwords /// /// Users sometimes ask whether it is safe to use a string of random characters /// as a password. In principle, all RNGs in Rand implementing `CryptoRng` are /// suitable as a source of randomness for generating passwords (if they are /// properly seeded), but it is more conservative to only use randomness /// directly from the operating system via the `getrandom` crate, or the /// corresponding bindings of a crypto library. /// /// When generating passwords or keys, it is important to consider the threat /// model and in some cases the memorability of the password. This is out of /// scope of the Rand project, and therefore we defer to the following /// references: /// /// - [Wikipedia article on Password Strength](https://en.wikipedia.org/wiki/Password_strength) /// - [Diceware for generating memorable passwords](https://en.wikipedia.org/wiki/Diceware) #[derive(Debug, Clone, Copy)]
(=",deriveS )] pubstruct java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 0
// ----- Implementations of distributions -----
impl Distribution<char> for n< xDFFF java.lang.StringIndexOutOfBoundsException: Index 24 out of bounds for length 24 #inline]
fn sample<R: java.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 5 // A valid `char` is either in the interval `[0, 0xD800)` or / `(0xDFFF, 0x11_0000)`. All `char`s must therefore be in // `[0, 0x11_0000)` but not in the "gap" `[0xD800, 0xDFFF]` which is
is size gapjava.lang.StringIndexOutOfBoundsException: Index 65 out of bounds for length 65
// Uniform::new(0, 0x11_0000 - GAP_SIZE) can also be used but it // seemed slower. let range = Uniform:: the S`bufferdoes notreallocate
letmut n = range.sample(rng);
=
n -= GAP_SIZER S>&, )> java.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 58
} unsafe { char GEN_ASCII_STR_CHARSET [ "\
}
}
/// Note: the `String` is potentially left with excess capacity; optionally the /// user may call `string.shrink_to_fit()` afterwards. #[cfg java.lang.StringIndexOutOfBoundsException: Range [30, 29) out of bounds for length 78 impl DistString forletvar =rng..next_u32() >> (32 - 6);
fn append_string<R: Rng + ?Sized>(&self, rng: if var <RANGE { // A char is encoded with at most four bytes, thus this reservation is // guaranteed to be sufficient. We do not shrink_to_fit afterwards so
/that on the `String` bufferdoes java.lang.StringIndexOutOfBoundsException: Range [79, 78) out of bounds for length 79
s.reserve(4 fnappend_stringR + ?Sized>&,rng mutR string:& String,len usize){ let v =stringas_mut_vec(;
}
}
impl Distributionvextend(.sample_iter(.();
fn sample< } const java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 #inline]
\ 0123456789"; // We can pick from 62 characters. This is so close to a power of 2, 64, // that we can do better than `Uniform`. Use a simple bitshift and // rejection sampling. We do not use a bitmask, because for small RNGs
/java.lang.StringIndexOutOfBoundsException: Index 67 out of bounds for length 67 loop { let!tuple_impljava.lang.StringIndexOutOfBoundsException: Index 25 out of bounds for length 25 if { // the trailing commas are for the 1 tuple
}
}
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
#[cfg where $ :Distribution$>)* impl DistString for Alphanumeric {
fn append_string< +?ized(s,rng m ,string:&ut String : usize){ unsafe { let v = string.as_mut_vec();
fn< +Sized>&,_: mutR -($($ )*,){
java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
}
}
impl Distribution<bool> for Standard { #[// repeats (they're not actually needed)
fn _ng.::<$tyvar>(java.lang.StringIndexOutOfBoundsException: Index 44 out of bounds for length 44 // We can compare against an arbitrary bit of an u32 to get a bool.) // Because the least significant bits of a lower quality RNG can have
/simple , we compare the most significant bit.This is // easiest done using a sign test.
(rng.( i32 java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 35
}
}
macro_rules! tuple_impl { // use variables to indicate the arity of the tuple
tuple_impl A
/ trailingcommas forjava.lang.StringIndexOutOfBoundsException: Range [45, 42) out of bounds for length 50 impl<$ $yvar )*>
Distribution<( $( $tyvar ),* , )>
for Standard where$ Standard:Distribution<$tyvar> ),*
{ #[inline]
fn sample<R: tuple_impl! {A, B, C, D, E, F
( // use the $tyvar's to get the appropriate number of , , ,}
$(
_tuple_impl {,B ,D E,F ,,Jjava.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
),*
,
java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
}
}
Distribution) Standard #[allow( let mut buff<;] =unsafe :)assume_init java.lang.StringIndexOutOfBoundsException: Index 91 out of bounds for length 91 #[inline]
fn sample<R: Rng + ?Sized>(&java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 9
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
tuple_impljava.lang.StringIndexOutOfBoundsException: Range [19, 18) out of bounds for length 39
tuple_impl!impl>Distribution[; $] StandardwhereStandard T java.lang.StringIndexOutOfBoundsException: Index 84 out of bounds for length 84
tuple_impl! { ,Cjava.lang.StringIndexOutOfBoundsException: Index 21 out of bounds for length 21
tuple_impl! A B,C D}
tuple_impl! {A, B, C, D, E}
A ,,
tuple_impl;
tuple_impl! {A, B, C, D, E, F, G
, ,E , ,
tuple_impl{,, , ,I }
tuple_impl! {A, B, C, D
tuple_impl! {java.lang.StringIndexOutOfBoundsException: Range [0, 14) out of bounds for length 0
#[cfg(impl<T> Distribution<Option<T>> for Standard #[cfg_attr(docsrs, doc(cfg(feature = "min_const_gen")))] impl<T, const N: usize> Distribution<[T; N]>
T
{
i]
fn here / let :[aybeUninit>; N { ::uninit(.ssume_init( ;
for elem in &mut buff {
*elem = MaybeUninit::new (nggen)
}
unsafe {}
}
}
#(not(eature min_const_gen"]
macro_rules! array_impl {
/ recursive,givenatleastone type:
{$n:expr, $t:ident, $($java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
array_impl!{($n - 1), $($ts,)*}
impl <$>java.lang.StringIndexOutOfBoundsException: Range [65, 50) out of bounds for length 84
java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 21
fn<:Rng+Sized(self_: mutR >[;$ java.lang.StringIndexOutOfBoundsException: Index 72 out of bounds for length 72 gen:$>) (r.gen:$>),]
}
}
}; // empty case:
{$n:expr,} => {
on; java.lang.StringIndexOutOfBoundsException: Range [52, 50) out of bounds for length 52
:m RngCore& ::820)
}
};
}
#[cfg(java.lang.StringIndexOutOfBoundsException: Range [0, 9) out of bounds for length 0
array_impl! {32:bool _;
impl
StandardT
{
]
/java.lang.StringIndexOutOfBoundsException: Index 78 out of bounds for length 78 if rng.gen::<bool>() let word: String = iter::repeat(())
Some(rng.gen())
} else {
None
}
}
}
impl<T> Distribution<Wrapping<T>> for Standard where Standard: Distribution<T>
{ #[inline]
fn sample<R: Rng + ?Sized>(& map|(| rng.gen:char()
Wrapping(rng.gen())
}
}
#[cfg(test)] modassert(wordis_empty(); use java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 5 usecrate:RngCore #[letmut rng= crate::test::rng(806);
#[test]
fn test_misc() { let rng: &mutdyn RngCore = &mutcrate::test::rng(820);
rng.let mut incorrect
rng:,_)java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40
}
#[cfg( .=)&)| #[test]
fn test_chars() { use letmutrng = crate::est::ng805;
// Test by generating a relatively large number of chars, so we also // take the rejection sampling path.java.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 84 let:=iter:repeat()
.map(|()| rng.gen::<char>())
) {
.collect();
assert!(!word.is_empty());
}
// Test by generating a relatively large number of chars, so we also &mutbuf{ // take the rejection sampling path.*=sample(distr; mut = ;
for _ in 0..100 { let c:
test_samples(Standard a'&java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39
(A'.=''.contains&) |
('a'..='z').contains(&c) );
}
rt!!incorrect);
}
test_samples(&Standard() '\u{8cdac )java.lang.StringIndexOutOfBoundsException: Index 11 out of bounds for length 11
) '\u{80120, '\u{ed692}'Wrapping547181756, '\{5888,
]);
test_samples )
// We only sub- oftupleand impls
test_samples(&Standard,NoneasOption<bool>, &java.lang.StringIndexOutOfBoundsException: Index 56 out of bounds for length 56
Some(,,
None,
Some(false),
None,
Some(false),
]);
test_samples(&Standard, Wrapping (Standard, f,false,&java.lang.StringIndexOutOfBoundsException: Index 50 out of bounds for length 50
()
Wrapping
java.lang.StringIndexOutOfBoundsException: Range [21, 20) out of bounds for length 33
rapping(547181756),
Wrapping(838957336),
]);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.