<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://wiki.lostsouls.org/w/skins/common/feed.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
		<title>Man rarity - Revision history</title>
		<link>http://wiki.lostsouls.org/w/index.php?title=Man_rarity&amp;action=history</link>
		<description>Revision history for this page on the wiki</description>
		<language>en</language>
		<generator>MediaWiki 1.8.2</generator>
		<lastBuildDate>Wed, 06 May 2026 18:28:47 GMT</lastBuildDate>
		<item>
			<title>Laine at 22:16, 11 June 2007</title>
			<link>http://wiki.lostsouls.org/w/index.php?title=Man_rarity&amp;diff=3998&amp;oldid=prev</link>
			<description>&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Files==&lt;br /&gt;
&lt;br /&gt;
    /lib/rarity.h&lt;br /&gt;
    /mod/global/rarity.c&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
&lt;br /&gt;
This document describes the lib's rarity mechanics.  These are mainly used&lt;br /&gt;
in applications of the selection parser (see 'man selection'), but can be&lt;br /&gt;
employed by other code as well.&lt;br /&gt;
&lt;br /&gt;
===Rarity Levels===&lt;br /&gt;
&lt;br /&gt;
rarity.h defines nine rarity levels, as follows:&lt;br /&gt;
&lt;br /&gt;
    Very Common&lt;br /&gt;
    Common&lt;br /&gt;
    Unusual&lt;br /&gt;
    Very Unusual&lt;br /&gt;
    Rare&lt;br /&gt;
    Very Rare&lt;br /&gt;
    Exotic&lt;br /&gt;
    Very Exotic&lt;br /&gt;
    Special&lt;br /&gt;
&lt;br /&gt;
The last level, Special, is not a true rarity level.  It is used for things&lt;br /&gt;
that should never be selected in any random pool, but nonetheless need a&lt;br /&gt;
rarity associated with them.  Some materials, for example, set this rarity&lt;br /&gt;
so that they will never show up randomly.&lt;br /&gt;
&lt;br /&gt;
The other levels indicate progressively increasing rarity.  The relationships&lt;br /&gt;
between the levels are as follows:&lt;br /&gt;
&lt;br /&gt;
    Common is half as likely as Very Common&lt;br /&gt;
    Unusual is half as likely as Common&lt;br /&gt;
    Very Unusual is two-fifths as likely as Unusual&lt;br /&gt;
    Rare is half as likely as Very Unusual&lt;br /&gt;
    Very Rare is half as likely as Rare&lt;br /&gt;
    Exotic is one-fifth as likely as Very Rare&lt;br /&gt;
    Very Exotic is half as likely as Exotic&lt;br /&gt;
&lt;br /&gt;
If you had a pool of at least one thing from each rarity level, with no&lt;br /&gt;
likelihood adjustments, the percentage chance of getting the thing from&lt;br /&gt;
each rarity level would be:&lt;br /&gt;
&lt;br /&gt;
    Very Common                     51.74%&lt;br /&gt;
    Common                          25.87%&lt;br /&gt;
    Unusual                         12.93%&lt;br /&gt;
    Very Unusual                     5.17%&lt;br /&gt;
    Rare                             2.58%&lt;br /&gt;
    Very Rare                        1.29%&lt;br /&gt;
    Exotic                           0.25%&lt;br /&gt;
    Very Exotic                      0.12%&lt;br /&gt;
&lt;br /&gt;
When working with pools that do not include all the rarity levels, the&lt;br /&gt;
base probabilities shift, according to the relative likelihood of each&lt;br /&gt;
level.  For instance, if you had only Very Common and Common things in&lt;br /&gt;
your pool, you would get a Very Common thing 2/3 of the time and a Common&lt;br /&gt;
thing the other 1/3.  If you had only items of Unusual rarity and above,&lt;br /&gt;
the percentages would be:&lt;br /&gt;
&lt;br /&gt;
    Unusual                         57.80%&lt;br /&gt;
    Very Unusual                    23.12%&lt;br /&gt;
    Rare                            11.56%&lt;br /&gt;
    Very Rare                        5.78%&lt;br /&gt;
    Exotic                           1.15%&lt;br /&gt;
    Very Exotic                      0.57%&lt;br /&gt;
&lt;br /&gt;
If you had only things of Rare rarity and above:&lt;br /&gt;
&lt;br /&gt;
    Rare                            60.60%&lt;br /&gt;
    Very Rare                       30.30%&lt;br /&gt;
    Exotic                           6.06%&lt;br /&gt;
    Very Exotic                      3.03%&lt;br /&gt;
&lt;br /&gt;
===Rarity Processing===&lt;br /&gt;
&lt;br /&gt;
Rarity processing is handled by /daemon/rarity, which publishes various&lt;br /&gt;
functions as services.  Always use the macros for these functions, as shown&lt;br /&gt;
below.  These functions are:&lt;br /&gt;
&lt;br /&gt;
mixed Rarity_Select(mapping select, mapping adjust)&lt;br /&gt;
&lt;br /&gt;
    Returns a randomly selected element of the mapping provided as the first&lt;br /&gt;
    argument, which should look something like this:&lt;br /&gt;
&lt;br /&gt;
        ([&lt;br /&gt;
            some_value           : Rarity_Common,&lt;br /&gt;
            some_other_value     : Rarity_Unusual,&lt;br /&gt;
            yet_another_value    : Rarity_Unusual,&lt;br /&gt;
        ])&lt;br /&gt;
&lt;br /&gt;
    The optional second argument (supply 0 if none) is a mapping that defines&lt;br /&gt;
    adjustments to the final likelihoods of individual elements, as described&lt;br /&gt;
    later on.  It might look like this:&lt;br /&gt;
&lt;br /&gt;
        ([&lt;br /&gt;
            some_value           : 1.5,&lt;br /&gt;
            some_other_value     : 0.1,&lt;br /&gt;
        ])&lt;br /&gt;
&lt;br /&gt;
mixed Rarity_Distribution(mapping select, mapping adjust, status allow_array)&lt;br /&gt;
&lt;br /&gt;
    Takes the same arguments as rarity_select() and returns the final mapping&lt;br /&gt;
    of elements to likelihoods.  &amp;quot;Likelihoods&amp;quot; are integer proportions, after&lt;br /&gt;
    the fashion used by random_element(); the mapping returned by this function&lt;br /&gt;
    is suitable to be passed directly to random_element().&lt;br /&gt;
&lt;br /&gt;
    If the third argument is true, then rarity_distribution() will be allowed&lt;br /&gt;
    to select randomly from among the rarity levels in its distribution and&lt;br /&gt;
    return the array of items from that level, rather than returning a full&lt;br /&gt;
    distribution.  This can only be done when no adjustment values are being&lt;br /&gt;
    provided.&lt;br /&gt;
&lt;br /&gt;
mapping Rarity_Analyze_Distribution(mapping distribution)&lt;br /&gt;
&lt;br /&gt;
    Takes an argument of a mapping containing values mapped to likelihoods&lt;br /&gt;
    (integer proportions), as returned by rarity_distribution() or macros like&lt;br /&gt;
    Selected_Materials_Distribution(), or as generally used with the function&lt;br /&gt;
    random_element().  The return value is a mapping of the argument's keys&lt;br /&gt;
    values mapped to float percentage chances of the value being selected by&lt;br /&gt;
    random_element().  Useful for getting an idea of the actual percentages&lt;br /&gt;
    you're winding up with from a likelihood distribution.&lt;br /&gt;
&lt;br /&gt;
mixed Rarity_Distribution_Relative(mapping select, mapping adjust)&lt;br /&gt;
&lt;br /&gt;
    Similar to rarity_distribution(), but rather than returning a mapping with&lt;br /&gt;
    integer proportions defining absolute weights, returns a mapping with&lt;br /&gt;
    float proportions defining relative weights.  The smallest proportion is&lt;br /&gt;
    arbitrarily assigned the value of 1.0, and the other proportions have&lt;br /&gt;
    values indicating their magnitude relative to that proportion.  This is&lt;br /&gt;
    mainly useful in cases where a rarity distribution is being used in&lt;br /&gt;
    conjunction with functions like distribute_proportionally(), and the&lt;br /&gt;
    values in play are large enough to potentially cause overflow errors if&lt;br /&gt;
    integers are used.&lt;br /&gt;
&lt;br /&gt;
mixed Rarity_Select_Simple(mapping select, mapping adjust)&lt;br /&gt;
&lt;br /&gt;
    Similar to Rarity_Select(), but does a simple distribution as described&lt;br /&gt;
    below.&lt;br /&gt;
&lt;br /&gt;
mixed Rarity_Distribution_Simple(mapping select, mapping adjust)&lt;br /&gt;
&lt;br /&gt;
    Similar to Rarity_Distribution(), but does a simple distribution as&lt;br /&gt;
    described below.&lt;br /&gt;
&lt;br /&gt;
mixed Rarity_Distribution_Simple_Relative(mapping select, mapping adjust)&lt;br /&gt;
&lt;br /&gt;
    Similar to Rarity_Distribution_Relative(), but does a simple distribution&lt;br /&gt;
    as described below.&lt;br /&gt;
&lt;br /&gt;
===How Rarity Becomes Likelihood===&lt;br /&gt;
&lt;br /&gt;
When rarity_distribution() turns a mapping of values to rarities into a&lt;br /&gt;
mapping of values to likelihoods, it normally does so in a way that gives&lt;br /&gt;
results that may be counterintuitive until you understand what it's doing.&lt;br /&gt;
&lt;br /&gt;
The first thing to understand is that, before adjustments are taken into&lt;br /&gt;
account, the chance of a value of a given rarity being selected is held&lt;br /&gt;
constant.  This means that no matter how many Common values you have in&lt;br /&gt;
your mapping, you have the same chance of getting a Common item.  You can&lt;br /&gt;
think of this almost as if the mechanism were selecting a rarity level&lt;br /&gt;
first, and then randomly selecting a value from within that rarity level&lt;br /&gt;
only.&lt;br /&gt;
&lt;br /&gt;
One thing this means is that, when looking at final percentages (as when&lt;br /&gt;
using analyze_distribution()), you can actually wind up with individual&lt;br /&gt;
values of Exotic rarity being more likely than individual values of Rare&lt;br /&gt;
rarity, because there are relatively few Exotic values and relatively&lt;br /&gt;
many Rare values.  The overall likelihood of the Rare category winds up&lt;br /&gt;
being split enough ways that the likelihood of individual values in the&lt;br /&gt;
category falls below the likelihood of individual Exotic values.&lt;br /&gt;
&lt;br /&gt;
This becomes more complex when likelihood adjustments are present, as our&lt;br /&gt;
next section explores.&lt;br /&gt;
&lt;br /&gt;
The exception to this is when using the &amp;quot;simple&amp;quot; distribution functions.&lt;br /&gt;
In these cases, the rarity values are simply used as weights, with no&lt;br /&gt;
attempt to hold the probability of rarity levels constant with respect to&lt;br /&gt;
each other.  This is most appropriate when you are dealing with a small&lt;br /&gt;
set of items in your rarity distribution, which would cause somewhat&lt;br /&gt;
distorted behavior when the probability manipulations described above are&lt;br /&gt;
used.&lt;br /&gt;
&lt;br /&gt;
===Likelihood Adjustments===&lt;br /&gt;
&lt;br /&gt;
The optional second argument to rarity_select() and rarity_distribution()&lt;br /&gt;
allows you to tune the likelihood of individual values in your rarity&lt;br /&gt;
map.  The adjustment argument is a map of values to float multipliers for&lt;br /&gt;
the value's likelihood, as shown above in the documentation for the&lt;br /&gt;
rarity_select() function.  If a value does not appear in the map, its&lt;br /&gt;
likelihood is not adjusted.&lt;br /&gt;
&lt;br /&gt;
The manipulations performed by the adjustment map violate the principle&lt;br /&gt;
of relative likelihood of rarity levels being held constant.  If you have&lt;br /&gt;
one Common value and one Unusual value, and adjust the Unusual value's&lt;br /&gt;
likelihood by 2.0, you wind up with the two values having the same&lt;br /&gt;
likelihood.  This is intentional, and done so that adjustments remain&lt;br /&gt;
meaningful across rarity levels.&lt;br /&gt;
&lt;br /&gt;
[ Example ]&lt;br /&gt;
&lt;br /&gt;
A quick example of a way you might use a rarity-based selection:&lt;br /&gt;
&lt;br /&gt;
    void configure() {&lt;br /&gt;
        string adj = Rarity_Select(([&lt;br /&gt;
            &amp;quot;happy&amp;quot; : Rarity_Common,&lt;br /&gt;
            &amp;quot;sad&amp;quot;   : Rarity_Uncommon,&lt;br /&gt;
            &amp;quot;angry&amp;quot; : Rarity_Rare,&lt;br /&gt;
        ]), 0);&lt;br /&gt;
        alter_identity(Identity_Adjectives, ({ adj }), True);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
For a more involved example, look at the randomize_ring_craft() function&lt;br /&gt;
in /std/app/rings/ordinary.c.  That shows a way you might use an adjustment&lt;br /&gt;
map set up according to situational factors to alter a basic rarity map.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[[man random element|random_element(sefun)]], [[man selection|selection(mechanisms)]], [[man selection core|selection_core(mechanisms)]]&lt;/div&gt;</description>
			<pubDate>Mon, 11 Jun 2007 22:16:14 GMT</pubDate>			<dc:creator>Laine</dc:creator>			<comments>http://wiki.lostsouls.org/Talk:Man_rarity</comments>		</item>
	</channel>
</rss>