# Algorithm for dynamically calculating a level based on experience points? [closed]

One of the struggles I've always had in game development is deciding how to implement experience points attributed to gaining a level. There doesn't seem to be a pattern to gaining a level in many of the games I've played, so I assume they have a static dictionary table which contains experience points vs. the level. e.g.

Experience   Level
0            1
100          2
175          3
280          4
800          5


...There isn't a rhyme or reason why 280 points is equal to level 4, it just is.

I'm not sure how those levels are decided, but it certainly wouldn't be dynamic. I've also thought about the possibility of exponential levels, as not to have to keep a separate lookup table, e.g.

Experience   Level
0            1
100          2
200          3
400          4
800          5
1600         6
3200         7
6400         8


...but that seems like it would grow out of control rather quickly, as towards the upper levels, the enemies in the game would have to provide a whopping amount of experience to level -- and that would be to difficult to control. Leveling would become an impossible task.

Does anyone have any pointers, or methods they use to decide how to level a character based on experience? I want to be fair in leveling and I want to stay ahead of the players as not to worry about constantly adding new experience/level lookups.

• No, it's just a slower exponential (multiplying by 10 every ~3.3 levels, or 100 every ~6.6 levels, instead of every level). Geometric would be like 1[00], 4, 9, 25, 36, etc. Even 1100 1210 1331 1464 is an exponential series, multiplying by 1.1. Jun 14 '11 at 17:31
• en.wikipedia.org/wiki/Exponential_growth Should clear things up. Jun 14 '11 at 20:03
• @Stephen: @Random832 is correct, both "exponential" and "geometric" growth refer to having a constant ratio between successive/equidistant terms (which means the rate of growth is proportional to the current value - in calculus this is stated by saying an exponential function has f'(x) = c*f(x)). The term "geometric" is used when the domain (input) is discrete (eg. integer). The formula for @George's example, above level 1, is Experience = 100 * 2^(Level-2), which is exponential. The formula for your progression, above level 1, is Experience = 100^(Level-1), also exponential. Jun 14 '11 at 20:58

It's quite common to use a square relation.

level = constant * sqrt(XP)

Or the approximate equivalent of a linearly rising level gap.

Level   XP      Difference
1       0       -
2       100     100
3       300     200
4       600     300
5       1000    400


These systems work pretty well when the XP gain is approximately linear. If a high level character can earn XP faster than a low level character then this is not the right system.

• For Achiever-type players, I suspect there is a strong expectation of greater XP gain as their character progresses. I find your solution interesting, and I'd probably tweak it only slightly to produce differences like 107-214-321-428, thus preventing the player from seeing only round numbers and also in the hopes that this could add to making the game feel just a little bit more "organic" (I'm trying to come up with a better word than this, but "organic" is all I've got at the moment). Jun 15 '11 at 6:05
• +1 this is exactly what I've done in the past, works great Jun 15 '11 at 16:08
• Equations for the linearly rising level gap. level = (sqrt(100(2experience+25))+50)/100 and experience =(level^2+level)/2*100-(level*100) Jan 27 '14 at 18:56
• Great answer and I like the curve this produces. I ended up using a constant of 0.04. For those wondering, the reverse of this formula to calculate for XP is: XP = level^2 / constant. Jun 24 '14 at 22:00
• @BradH Actually, it seems to be XP = (level / constant)^2. In php, this can be represented as XP = pow(level / constant, 2) or in later versions, (level / constant)**2 May 16 '15 at 15:29

You'll probably find logarithmic functions helpful as they can be used to slow down the increase as more experience points are gained. Here are two web pages that explain how these work:

Graphs of Logarithmic Functions
http://www.analyzemath.com/Graphing/GraphLogarithmicFunction.html

Introduction to Graphing Logarithmic Functions
http://www.purplemath.com/modules/graphlog.htm

Here's what it looks like on a graph, which will hopefully meet your needs:

• @George By playing around with the co-efficients you can easily use a logarithmic function to scale your player's levelling. +1 for using math Jun 14 '11 at 17:27
• Note that this is an implementation of exponential XP requirement. level = log(XP) <=> XP = e^level. The requirement may turn out a bit steep for the last levels, but it will work ok with a suitable growth in XP accumulation. Jun 14 '11 at 19:57
• @eBusiness: Good point, but this may not be a problem since higher level characters tend to earn more points more easily and in a shorter duration of time (e.g., by attacking/killing more powerful monsters with a faster hit speed). This is one reason why I think this math could be well-suited to video games (especially in an MMORPG context). Jun 14 '11 at 23:21
• I just find it a bit odd that OP specifically worry about exponential growth and then accept an answer that just describe the same thing in another way. But I guess that is his problem. Jun 15 '11 at 10:20
• @eBusiness: Perhaps my answer clarified something about exponential growth that wasn't previously clear? There are many different ways to apply mathematics, and I'm not sure that even the best math teacher could know them all (well, maybe the second-best math teacher anyway). Jun 15 '11 at 16:22

These are some ways I've used so far. They are sorted by grow rate (just multiplied every equation by a constant value):

1. exp = level (this one may seem odd but for some games that reward exp changes according to player level like borderlands this one may fit to)

2. exp = level * log10(level)

1   0
2   0.6
3   1.4
4   2.4
5   3.5
6   4.6

3. exp = level^const_value : for my example I set const value to 3/2

1   1
2   2.8
3   5.1
4   8
5   11.2
6   14.7


I think it depends how how easy the player can get the experience. If the player can get 200 xp at lvl 1 in 20 minutes and then get 200 xp at lvl 2 in 2 minutes, then you need to scale your xp exponentially.

However if gaining a level only makes it marginally easier to gain xp, then you should do a more level xp function. (Maybe linear with some coefficient > 1)

Often the points needed to gain a level is based on a curve. In a lot of RPGs were there are also a lot of other characteristics and attributes of your character that increase with your level, they will also be on there own curves.

Take a look at RPGMaker to see how they do things. Specifically look at the parameter curves in the screenshots of the "Add Some Uniqueness to by Customizing Starting Equipment" section of the Creating a Main Character document.

Basically you might have a curve for how much XP is needed to levelup, then different curves for how much HP, MP, stamina, strength etc is gained each level.

Consider Time

You first need to figure out how the rate of experience gain scales with level. Then you can tailor your leveling by time required. If every additional level allows you to gain experience twice as fast, and you need twice as much experience to level, every level will require the same amount of time.

My suggestion is to think about how much time the player should spend, then tailor the leveling to that.

Maybe I should just use a flat leveling system, where each level would require exactly the same amount of experience.

• Answers should be a bit more fleshed out than just a simple suggestion. Try relating some of the benefits of this strategy over having a dynamic system like the one asked for.
– House
Oct 31 '13 at 19:46
• Also note that additional information should be posted by the original author; if other users have more to add, they should consider posting a separate answer. Jul 12 '17 at 4:56