Pick a Random Card — Randomized

Build a dealer that returns a card per call from a small deck, every card equally likely each time, with dealt cards returned to the deck.

Randomized · Easy

The problem

A card game deals one card at a time from a small deck. Build a class over a list of distinct cards. Each call to draw() returns one card chosen uniformly at random. The card goes back into the deck every time, so every call is independent and every card is equally likely on every call.

The story

A card game deals one card at a time from a tiny deck of 1 to 4 different cards, each numbered from -9 to 9. For every deal, each card must be equally likely, and the dealt card goes straight back into the deck, so the next deal is a fresh draw from the whole deck. Build the dealer so that each call picks one card at random.

What to return

Constraints

Example

Input
Solution([2,-1,3]), draw(), draw(), draw()
Output
—

Solve it

More in Randomized

All algorithm problems