Crc32()

From TinyMUX
Jump to: navigation, search

Description

FUNCTION: 20px-Function.png crc32(string1, ..., stringN)

Returns the Cyclic Redundancy Check of the strings or sequences of strings as a positive number. The answer for the same sequence of strings is always the same.

The usefulness of this number is broad, but could provide a unique fingerprint, checksum pattern, or high-quality hash function.

Examples

> think crc32(MUX)
629126998


(--Soylent 01:15, 3 December 2005 (EST))

Nifty tricks

Why would you need a hash function in softcode anyways? While the description is suggestive of many potential applications, nothing useful and specific is mentioned.

Nifty trick #1

Suppose you're creating a large bank of records, each indexed with a number or key word. There's a maximum attribute limit, and you're reaching it because you have so many records.

One solution is to split up the records evenly between multiple objects. But what's an easy way to do this? How do you know by the index which object to look at for the attribute? The answer is to hash the index with crc32()! If you want to know which object and attribute an index's corresponding data could be found, the code might look like this:

@@ List of objects that the database is spread over
@vo database=#853 #191 #1210
@@ Number of objects that the database is spread over
@vn database=3
&fn.indexToAttribute database=extract(%vo,inc(mod(crc32(%0),%vn)),1)/d.%0

An example of its use:

> th u(database/fn.indexToAttribute,pie)
#1210/d.pie
> th u(database/fn.indexToAttribute,tastes)
#853/d.tastes
> th u(database/fn.indexToAttribute,good)
#1201/d.good
> th u(database/fn.indexToAttribute,Good)
#853/d.Good

Keep in mind that crc32() is case sensitive. If you want to create case insensitive behavior, use lcstr() or ucstr() within it: e.g. crc32(lcstr(%0))

Nifty trick #2

Often it is handy to create a whole set of random data that is prohibitively large. This is typically true for weather systems or almost any type of comprehensive simulation. Crc32() chews on a string and churns out a number. So, if you feed it a time (typically represented in seconds), it can spit out a corresponding number.

While this may sound simplistic, it is especially hard to see where it applies. Anytime you're attempting to model a process over a large range of scales (from seconds to years, from centimeters to kilometers), you'll have a hard time accounting for all the detail without using a clever trick. A hash function or altered hash function usually serves well.

How dense is the forest in this section of land?
How cold will this room be in an hour?
What is the chance that gold drops here?
What is the projection of the IC stock market?
How many warp particles are floating in this region of space?

Look up Perlin noise for more ideas.

Nifty trick #3

What's the crc32() of 2733338650?

--Ian