Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A "hash" is the solution created to solve <strong>exactly</strong> the problem you are describing. See <a href="http://en.wikipedia.org/wiki/Hash_function" rel="nofollow noreferrer">wikipedia's article</a></p> <p>Any hash function you use will be nice; hash functions tend to be judged based on these criteria:</p> <ul> <li>The degree to which they prevent <em>collisions</em> (two separate inputs producing the same output) -- a by-product of this is the degree to which the function minimizes outputs that may never be reached from any input.</li> <li>The uniformity the distribution of its outputs given a uniformly distributed set of inputs</li> <li>The degree to which small changes in the input create large changes in the output.</li> </ul> <p>(see <a href="http://en.wikipedia.org/wiki/Perfect_hash_function" rel="nofollow noreferrer">perfect hash function</a>)</p> <p>Given how hard it is to create a hash function that maximizes all of these criteria, why not just use one of the most commonly used and relied-on existing hash functions there already are?</p> <p>From what it seems, turning integers into strings almost seems like another layer of encryption! (which is good for your purposes, I'd assume)</p> <p>However, your question asks for hash functions that deal <strong>specifically with numbers</strong>, so here we go.</p> <hr> <h1>Hash functions that work over the integers</h1> <p>If you want to borrow already-existing algorithms, you may want to dabble in <strong><a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator" rel="nofollow noreferrer">pseudo-random number generators</a></strong></p> <p>One simple one is the middle square method:</p> <ul> <li>Take a digit number</li> <li>Square it</li> <li>Chop off the digits and leave the middle digits with the same length as your original.</li> </ul> <p>ie,</p> <pre><code>1111 =&gt; 01234321 =&gt; 2342 </code></pre> <p>so, 1111 would be "hashed" to 2342, in the middle square method.</p> <p>This way isn't <strong>that</strong> effective, but for a few number of hashes, this has very low collision rates, a uniform distribution, and great chaos-potential (small changes => big changes). But if you have many values, time to look for something else...</p> <p>The grand-daddy of all feasibly efficient and simple random number generators is the (Mersenne Twister)[<a href="http://en.wikipedia.org/wiki/Mersenne_twister]" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Mersenne_twister]</a>. In fact, an implementation is probably out there for every programming language imaginable. Your hash "input" is something that will be called a "seed" in their terminology.</p> <p>In conclusion</p> <ol> <li>Nothing wrong with string-based hash functions</li> <li>If you want to stick with the integers and be fancy, try using your number as a seed for a pseudo-random number generator.</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload