Skip to main content

Rng

Struct Rng 

Source
pub struct Rng { /* private fields */ }
Expand description

Oddiy, tez va deterministik pseudo-random generator (xorshift64*).

Diqqat: kriptografiya uchun yaramaydi. Faqat demo/benchmark uchun.

§Misol

use rust_algorithms::util::Rng;

let mut rng = Rng::new(42);
let a: Vec<u64> = (0..5).map(|_| rng.next_u64() % 100).collect();
let mut rng2 = Rng::new(42);
let b: Vec<u64> = (0..5).map(|_| rng2.next_u64() % 100).collect();
assert_eq!(a, b); // bir xil urug' (seed) → bir xil ketma-ketlik

Implementations§

Source§

impl Rng

Source

pub fn new(seed: u64) -> Self

Yangi generator yaratadi. seed == 0 bo’lsa, avtomatik almashtiriladi.

Source

pub fn next_u64(&mut self) -> u64

Navbatdagi 64-bitli tasodifiy son.

Source

pub fn below(&mut self, n: usize) -> usize

0..n oralig’idagi tasodifiy usize (n > 0 bo’lishi shart).

Source

pub fn range(&mut self, lo: i64, hi: i64) -> i64

lo..=hi oralig’idagi tasodifiy i64.

Source

pub fn shuffle<T>(&mut self, slice: &mut [T])

Slicening elementlarini joyida aralashtiradi (Fisher–Yates, O(n)).

§Misol
use rust_algorithms::util::Rng;

let mut v: Vec<i32> = (0..10).collect();
Rng::new(7).shuffle(&mut v);
let mut sorted = v.clone();
sorted.sort();
assert_eq!(sorted, (0..10).collect::<Vec<_>>()); // elementlar yo'qolmadi
Source

pub fn vec(&mut self, n: usize, lo: i64, hi: i64) -> Vec<i64>

n ta tasodifiy i64 dan iborat vektor.

Auto Trait Implementations§

§

impl Freeze for Rng

§

impl RefUnwindSafe for Rng

§

impl Send for Rng

§

impl Sync for Rng

§

impl Unpin for Rng

§

impl UnsafeUnpin for Rng

§

impl UnwindSafe for Rng

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.