Spooky Sale: 10% off Premium yearly · $118.80 for the first year · until Nov 6Get it

Deep-ML
Learn
Problems1,200+ hands-on ML problemsLearning PathA guided route through the fundamentalsLabsInteractive labs on real techniquesProjectsBuild a GPT, an RL agent, CUDA kernelsCollectionsCurated problem sets and videosMathPen-and-paper math for MLIntuitionInvent the idea behind the formula yourselfPlaylistsYour own problem lists, shareableStudy AssistantAn AI teacher that knows your progress
Interview Prep
Compete
ContestsFresh problem sets, ranked liveLeaderboardWhere you stand, globallyBuddiesSolve with a friend: streaks, a weekly board, chat
Discuss
Deep-Path
Premium
      Intuition
      34 of 102

      Roll a loaded die

      A loaded die doesn’t land on every outcome equally often: chances[i] is how often outcome i comes up, and the chances add up to 1. A computer can only roll fair: random.random() gives a roll, a number from 0 up to (not including) 1, every value equally likely. Write pick(chances, roll): the outcome (its index, from 0) that this roll gives, so that over many rolls each outcome comes up as often as its chance. The same roll always gives the same outcome.

      A loaded die as a wheel: one wedge per outcome, each as wide as its chance. The computer can’t spin a loaded wheel; all it has is a fair roll, a number from 0 up to 1. That roll is how far round the rim the wheel stops. Your pick() turns a roll into an outcome.

      A wedge per outcome, as wide as its chance: 50%, 30%, 20%.01200.10.20.30.40.50.60.70.80.90 is straight up · clockwise to 1the fair roll: 0 up to 1, all equally likely00.250.50.7510.62each outcome’s chanceoutcome 0: 50%outcome 1: 30%outcome 2: 20%pick([0.5, 0.3, 0.2], roll 0.62) →?
      Your functionpick(chances, roll)
      chanceshow often each outcome comes up, adding up to 1e.g. [0, 1, 0]
      rolla fair random number, 0 <= roll < 1e.g. 0.05
      returnsthe outcome (an index) this roll gives
      solution.pyLoading Python
      Sign in for an AI hint
      ← Too big for the computerCut the long tail →