Code Explainers

Code explainers tagged #randomness

python
import random
from typing import Iterator, List
 
 

How reservoir sampling picks k items

reservoir-sampling streaming randomness
Intermediate 5 steps
rust
use rand::Rng;
 
#[derive(Debug)]
pub struct WeightedChoice<T> {

Weighted random sampling in Rust

weighted-sampling generics binary-search
Intermediate 7 steps
rust
use rand::distributions::{Alphanumeric, DistString};
use rand::rngs::OsRng;
 
#[derive(Debug, Clone, PartialEq, Eq)]

A newtype wrapper for API tokens in Rust

newtype-pattern randomness encapsulation
Intermediate 6 steps
go
package token
 
import (
	"crypto/rand"

Generating secure random tokens in Go

cryptography randomness encoding
Intermediate 5 steps