A function where small changes in input always result in large changes in output

China☆狼群 提交于 2019-12-05 11:36:57
Justin L.

A "hash" is the solution created to solve exactly the problem you are describing. See wikipedia's article

Any hash function you use will be nice; hash functions tend to be judged based on these criteria:

  • The degree to which they prevent collisions (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.
  • The uniformity the distribution of its outputs given a uniformly distributed set of inputs
  • The degree to which small changes in the input create large changes in the output.

(see perfect hash function)

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?

From what it seems, turning integers into strings almost seems like another layer of encryption! (which is good for your purposes, I'd assume)

However, your question asks for hash functions that deal specifically with numbers, so here we go.


Hash functions that work over the integers

If you want to borrow already-existing algorithms, you may want to dabble in pseudo-random number generators

One simple one is the middle square method:

  • Take a digit number
  • Square it
  • Chop off the digits and leave the middle digits with the same length as your original.

ie,

1111 => 01234321 => 2342

so, 1111 would be "hashed" to 2342, in the middle square method.

This way isn't that 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...

The grand-daddy of all feasibly efficient and simple random number generators is the (Mersenne Twister)[http://en.wikipedia.org/wiki/Mersenne_twister]. 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.

In conclusion

  1. Nothing wrong with string-based hash functions
  2. If you want to stick with the integers and be fancy, try using your number as a seed for a pseudo-random number generator.

Hashing fits your requirements perfectly. If you really don't want to use strings, find a Hash library that will take numbers or binary data. But using strings here looks OK to me.

Bob Jenkins' mix function is a classic choice, at when n=3.

As others point out, hash functions do exactly what you want. Hashes take bytes - not character strings - and return bytes, and converting between integers and bytes is, of course, simple. Here's an example python function that works on 32 bit integers, and outputs a 32 bit integer:

import hashlib
import struct

def intsha1(ints):
  input = struct.pack('>%di' % len(ints), *ints)
  output = hashlib.sha1(input).digest()
  return struct.unpack('>i', output[:4])

It can, of course, be easily adapted to work with different length inputs and outputs.

Have a look at this, may be you can be inspired

Chaotic system

In chaotic dynamics, small changes vary results greatly.

A x-bit block cipher will take an number and convert it effectively to another number. You could combine (sum/mult?) your input numbers and cipher them, or iteratively encipher each number - similar to a CBC or chained mode. Google 'format preserving encyption'. It is possible to create a 32-bit block cipher (not widely 'available') and use this to create a 'hashed' output. Main difference between hash and encryption, is that hash is irreversible.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!