Crates.io | rdeck |
lib.rs | rdeck |
version | 0.2.0 |
source | src |
created_at | 2023-12-27 22:14:15.341614 |
updated_at | 2023-12-27 22:14:15.341614 |
description | a simple library for choosing distinct random elements |
homepage | |
repository | https://git.envs.net/binarycat/rdeck |
max_upload_size | |
id | 1081929 |
size | 10,912 |
most common usecases should be covered by calling to_deck on a range or slice.
once you have the deck, simply call deck.draw()
as many times as you wish (or try_draw()
if there is a chance you will run out of elements in the deck).
say you are trying to select two random integers, and you want them to be:
there are a number of trivial solutions, but all of them have problems:
the basic principle is instead of having a list of items that haven't been selected, you have a list of items that have been selected.
this means the memory and cpu complexity are proportional to the number of elements being selected instead of the number of potential elements.
it also makes it trivial to reset the deck to its original state.
in practice, only IntDeck
does this, and everything else is simply built on top of IntDeck.