# [−][src]Struct rand::chacha::ChaChaRng

    pub struct ChaChaRng { /* fields omitted */ }

A random number generator that uses the ChaCha20 algorithm [1].

The ChaCha algorithm is widely accepted as suitable for cryptographic purposes, but this implementation has not been verified as such. Prefer a generator like `OsRng` that defers to the operating system for cases that need high security.

[1]: D. J. Bernstein, ChaCha, a variant of Salsa20

## Methods

### `impl ChaChaRng`[src]

#### `pub fn new_unseeded() -> ChaChaRng`[src]

Create an ChaCha random number generator using the default fixed key of 8 zero words.

# Examples

    use rand::{Rng, ChaChaRng};

    let mut ra = ChaChaRng::new_unseeded();
    println!("{:?}", ra.next_u32());
    println!("{:?}", ra.next_u32());

Since this equivalent to a RNG with a fixed seed, repeated executions of an unseeded RNG will produce the same result. This code sample will consistently produce:

- 2917185654
- 2419978656

#### `pub fn set_counter(&mut self, counter_low: u64, counter_high: u64)`[src]

Sets the internal 128-bit ChaCha counter to a user-provided value. This permits jumping arbitrarily ahead (or backwards) in the pseudorandom stream.

Since the nonce words are used to extend the counter to 128 bits, users wishing to obtain the conventional ChaCha pseudorandom stream associated with a particular nonce can call this function with arguments `0, desired_nonce`.

# Examples

    use rand::{Rng, ChaChaRng};

    let mut ra = ChaChaRng::new_unseeded();
    ra.set_counter(0u64, 1234567890u64);
    println!("{:?}", ra.next_u32());
    println!("{:?}", ra.next_u32());

## Trait Implementations

### `impl Rand for ChaChaRng`[src]

#### `fn rand<R: Rng>(other: &mut R) -> ChaChaRng`[src]

Generates a random instance of this type using the specified source of randomness. Read more

### `impl Rng for ChaChaRng`[src]

#### `fn next_u32(&mut self) -> u32`[src]

Return the next random u32. Read more

#### `fn next_u64(&mut self) -> u64`[src]

Return the next random u64. Read more

#### `fn next_f32(&mut self) -> f32`[src]

Return the next random f32 selected from the half-open interval `[0, 1)`. Read more

#### `fn next_f64(&mut self) -> f64`[src]

Return the next random f64 selected from the half-open interval `[0, 1)`. Read more

#### `fn fill_bytes(&mut self, dest: &mut [u8])`[src]

Fill `dest` with random data. Read more

#### `fn gen<T: Rand>(&mut self) -> T where
Self: Sized,` [src]

Return a random value of a `Rand` type. Read more

####
ⓘImportant traits for Generator<'a, T, R>

### Important traits for Generator<'a, T, R>

`impl<'a, T: Rand, R: Rng> Iterator for Generator<'a, T, R> type Item = T;`
`fn gen_iter<'a, T: Rand>(&'a mut self) -> Generator<'a, T, Self> where
Self: Sized,` [src]

Return an iterator that will yield an infinite number of randomly generated items. Read more

#### `fn gen_range<T: PartialOrd + SampleRange>(&mut self, low: T, high: T) -> T where
Self: Sized,` [src]

Generate a random value in the range [`low`, `high`). Read more

#### `fn gen_weighted_bool(&mut self, n: u32) -> bool where
Self: Sized,` [src]

Return a bool with a 1 in n chance of true Read more

####
ⓘImportant traits for AsciiGenerator<'a, R>

### Important traits for AsciiGenerator<'a, R>

`impl<'a, R: Rng> Iterator for AsciiGenerator<'a, R> type Item = char;`
`fn gen_ascii_chars<'a>(&'a mut self) -> AsciiGenerator<'a, Self> where
Self: Sized,` [src]

Return an iterator of random characters from the set A-Z,a-z,0-9. Read more

#### `fn choose<'a, T>(&mut self, values: &'a [T]) -> Option<&'a T> where
Self: Sized,` [src]

Return a random element from `values`. Read more

#### `fn choose_mut<'a, T>(&mut self, values: &'a mut [T]) -> Option<&'a mut T> where
Self: Sized,` [src]

Return a mutable pointer to a random element from `values`. Read more

#### `fn shuffle<T>(&mut self, values: &mut [T]) where
Self: Sized,` [src]

Shuffle a mutable slice in place. Read more

### `impl<'a> SeedableRng<&'a [u32]> for ChaChaRng`[src]

#### `fn reseed(&mut self, seed: &'a [u32])`[src]

Reseed an RNG with the given seed. Read more

#### `fn from_seed(seed: &'a [u32]) -> ChaChaRng`[src]

Create a ChaCha generator from a seed, obtained from a variable-length u32 array. Only up to 8 words are used; if less than 8 words are used, the remaining are set to zero.

### `impl Clone for ChaChaRng`[src]

#### `fn clone(&self) -> ChaChaRng`[src]

Returns a copy of the value. Read more

#### `fn clone_from(&mut self, source: &Self)`1.0.0[src]

Performs copy-assignment from `source`. Read more

### `impl Copy for ChaChaRng`[src]

### `impl Debug for ChaChaRng`[src]

#### `fn fmt(&self, f: &mut Formatter) -> Result`[src]

Formats the value using the given formatter. Read more

## Auto Trait Implementations

### `impl Send for ChaChaRng`

### `impl Unpin for ChaChaRng`

### `impl Sync for ChaChaRng`

### `impl UnwindSafe for ChaChaRng`

### `impl RefUnwindSafe for ChaChaRng`

## Blanket Implementations

### `impl<T> ToOwned for T where
T: Clone,` [src]

#### `type Owned = T`

The resulting type after obtaining ownership.

#### `fn to_owned(&self) -> T`[src]

Creates owned data from borrowed data, usually by cloning. Read more

#### `fn clone_into(&self, target: &mut T)`[src]

🔬 This is a nightly-only experimental API. (`toowned_clone_into`)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `fn into(self) -> U`[src]

Performs the conversion.

### `impl<T> From<T> for T`[src]

#### `fn from(t: T) -> T`[src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`[src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

#### `type Error = <U as TryFrom<T>>::Error`

The type returned in the event of a conversion error.

#### `fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`[src]

Performs the conversion.

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `fn borrow_mut(&mut self) -> &mut T`[src]

Mutably borrows from an owned value. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `fn borrow(&self) -> &T`[src]

Immutably borrows from an owned value. Read more

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `fn type_id(&self) -> TypeId`[src]

Gets the `TypeId` of `self`. Read more

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)