serhii.net

In the middle of the desert you can say anything you want

18 Feb 2024

python random sample vs random choices

Got bit by this.

random — Generate pseudo-random numbers — Python 3.12.2 documentation

  • SAMPLE() (random.sample()) IS WITHOUT REPLACEMENT: no duplicates unless present in list (random.shuffle())
  • CHOICES() (random.choices()) IS WITH REPLACEMENT: duplicates MAY happen.

Also:

  • random.shuffle() works in-place. Sampling len(x) is a way to shuffle immutable lists.
Nel mezzo del deserto posso dire tutto quello che voglio.