When you click the button for a level 17 enchantment, how does Minecraft decide to apply Sharpness II? Here's an explanation. But first, some terminology:

The enchantment algorithm uses a three-step process.

Step One

Apply modifiers to the enchantment level

The first thing that Minecraft does is apply two modifiers to the base enchantment level (the green number, and the number of experience levels you actually pay). Each modifier is restricted to a certain range, with numbers close to the middle of the range more common than those near the ends.

The first modifier is based on the item's "enchantability," which depends on the material and the type of the item (see the table below). Minecraft picks a number between 0 and half of the enchantability (with enchantability / 4 the most probable result), then adds that number plus one to the enchantment level.

ArmorSword
or Tool
Wood15
Leather15
Stone5
Iron914
Chain12
Diamond1010
Gold2522

Bows and books have enchantability 1.

Next, Minecraft picks a value between 0.85 and 1.15, with values closer to 1.0 being more likely. The enchantment level is multiplied by this value (so it increases or decreases by up to 15%) and then rounded to the nearest integer.

Step Two

Find possible enchantments

Now, based on the modified level, Minecraft makes a list of all enchantment types that can be applied to the target item along with the power that each enchantment will have.

The power of each enchantment type is determined by the level and the values in the table below. For each power value of an enchantment type, there is a minimum and maximum level that can produce the enchantment at that power. If the modified enchantment level is within the range, then the enchantment will be assigned that power. If the level is within two overlapping ranges for the same enchantment type, the higher power value is used.

IIIIIIIVV
Protection121123223433454--
Fire Protection1022183026383446--
Feather Fall515112117272333--
Blast Protection517132521332941--
Projectile Protection31892415302136--
Respiration104020503060----
Aqua Affinity141--------
Thorns1060308050100----
Sharpness1211232234334544565
Smite5251333214129493757
Bane of Arthropods5251333214129493757
Knockback5552575------
Fire Aspect10603080------
Looting156524743383----
Efficiency1511161217131814191
Silk Touch1565--------
Unbreaking55513632171----
Fortune156524743383----
Power1161126213631464156
Punch12373257------
Flame2050--------
Infinity2050--------

An example: Suppose we are enchanting a sword with a modified enchantment level of 21. If the sword gets the Sharpness enchantment, it will get Sharpness II because 21 is in the power-II range for Sharpness (12-32). 21 is also in the power-I range (1-21), but the higher power value will win over the lower. If the sword gets Smite, then it will be Smite III, because 21 is in the power-III range for Smite (21-41).

Note that extremely high modified enchantment levels can fall outside the ranges of all valid enchantments for the item. If this happens, the item is left unenchanted, but you are not charged any experience levels and can try enchanting it again.

Step Three

Select a set of enchantments from the list

Now that it's got a list of the possible enchantments for the item, Minecraft has to pick some of them that will actually be applied.

You always get at least one enchantment. The first enchantment is picked from the list based on the weights below.

Weight
Protection10
Fire Protection5
Feather Fall5
Blast Protection2
Projectile Protection5
Respiration2
Aqua Affinity2
Thorns1
Sharpness10
Smite5
Bane of Arthropods5
Knockback5
Fire Aspect2
Looting2
Efficiency10
Silk Touch1
Unbreaking5
Fortune2
Power10
Punch2
Flame2
Infinity1

For example, suppose the item is a piece of armor with both Protection and Respiration available. Protection has 10 chances to be selected compared to 2 for Respiration, so the probability of getting Protection is about 5 times higher than the probability of Respiration.

After the first element is selected, there is a chance of receiving more, based on this algorithm:

  1. With probability (modified level + 1) / 50, keep going. Otherwise, stop picking bonus enchantments.
  2. Remove from the list of possible enchantments anything that conflicts with previously-chosen enchantments. For example, if Smite was already picked, Sharpness and Bane of Arthropods will be removed at this step. See below for more info on conflicts.
  3. Pick one enchantment from the remaining possible enchantments (based on the weights, as before) and apply it to the item.
  4. Divide the modified level in half (rounded down) and repeat from step 1.

When enchanting a book, if this process selects more than one enchantment, the game chooses one enchantment at random to keep and discards the rest.

Here are the rules for enchantment conflicts: