Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7614691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:28:07+00:00 2026-05-31T02:28:07+00:00

I need a quick hash function for integers: int hash(int n) { return …;

  • 0

I need a quick hash function for integers:

 int hash(int n) { return ...; }

Is there something that exists already in Java?

The minimal properties that I need are:

  • hash(n) & 1 does not appear periodic when used with a bunch of consecutive values of n.
  • hash(n) & 1 is approximately equally likely to be 0 or 1.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:28:08+00:00Added an answer on May 31, 2026 at 2:28 am

    I did some experimentation (see test program below); computation of 2^n in Galois fields, and floor(A*sin(n)) both did very well to produce a sequence of “random” bits. I tried multiplicative congruential random number generators and some algebra and CRC (which is analogous of k*n in Galois fields), none of which did well.

    The floor(A*sin(n)) approach is the simplest and quickest; the 2^n calculation in GF32 takes approx 64 multiplies and 1024 XORs worstcase, but the periodicity of output bits is extremely well-understood in the context of linear-feedback shift registers.

    package com.example.math;
    
    public class QuickHash {
        interface Hasher
        {
            public int hash(int n); 
        }
        static class MultiplicativeHasher1 implements Hasher
        {
            /* multiplicative random number generator
             * from L'Ecuyer is x[n+1] = 1223106847 x[n] mod (2^32-5)
             * http://dimsboiv.uqac.ca/Cours/C2012/8INF802_Hiv12/ref/paper/RNG/TableLecuyer.pdf
             */
            final static long a = 1223106847L;
            final static long m = (1L << 32)-5;
            /*
             * iterative step towards computing mod m
             *   (j*(2^32)+k) mod (2^32-5)
             * = (j*(2^32-5)+j*5+k) mod (2^32-5)
             * = (j*5+k) mod (2^32-5)
             * repeat twice to get a number between 0 and 2^31+24 
             */
            private long quickmod(long x)
            {
                long j = x >>> 32;
                long k = x & 0xffffffffL;
                return j*5+k;
            }
            // treat n as unsigned before computation
            @Override public int hash(int n) {
                long h = a*(n&0xffffffffL);
                long h2 = quickmod(quickmod(h));            
                return (int) (h2 >= m ? (h2-m) : h2);
            }       
            @Override public String toString() { return getClass().getSimpleName(); } 
        }
    
        /** 
         * computes (2^n) mod P where P is the polynomial in GF2
         * with coefficients 2^(k+1) represented by the bits k=31:0 in "poly";
         * coefficient 2^0 is always 1
         */
        static class GF32Hasher implements Hasher
        {
            static final public GF32Hasher CRC32 = new GF32Hasher(0x82608EDB, 32);
    
            final private int poly;
            final private int ofs;
            public GF32Hasher(int poly, int ofs) {
                this.ofs = ofs;
                this.poly = poly;
            }
    
            static private long uint(int x) { return x&0xffffffffL; }
            // modulo GF2 via repeated subtraction
            int mod(long n) {
                long rem = n;
                long q = uint(this.poly);
                q = (q << 32) | (1L << 31);
                long bitmask = 1L << 63;
                for (int i = 0; i < 32; ++i, bitmask >>>= 1, q >>>= 1)
                {
                    if ((rem & bitmask) != 0)
                        rem ^= q;
                }
                return (int) rem;
            }
            int mul(int x, int y)
            {
                return mod(uint(x)*uint(y));
            }
            int pow2(int n) {
                // compute 2^n mod P using repeated squaring
                int y = 1;
                int x = 2;
                while (n > 0)
                {
                    if ((n&1) != 0)
                        y = mul(y,x);
                    x = mul(x,x);
                    n = n >>> 1;
                }
                return y;
            }
            @Override public int hash(int n) {
                return pow2(n+this.ofs);
            }
            @Override public String toString() { 
                return String.format("GF32[%08x, ofs=%d]", this.poly, this.ofs); 
            }
        }
        static class QuickHasher implements Hasher
        {
            @Override public int hash(int n) {
                return (int) ((131111L*n)^n^(1973*n)%7919);
            }
            @Override public String toString() { return getClass().getSimpleName(); }       
        }
    
        // adapted from http://www.w3.org/TR/PNG-CRCAppendix.html
        static class CRC32TableHasher implements Hasher
        {
            final private int table[];
            static final private int polyval = 0xedb88320;
    
            public CRC32TableHasher() 
            {           
                this.table = make_table();
            }
    
            /* Make the table for a fast CRC. */
            static public int[] make_table()
            {
                int[] table = new int[256]; 
                int c;
                int n, k;
    
                for (n = 0; n < 256; n++) {
                    c = n;
                    for (k = 0; k < 8; k++) {
                        if ((c & 1) != 0)
                            c = polyval ^ (c >>> 1);
                        else
                            c = c >>> 1;
                    }
                    table[n] = (int) c;
                }
                return table;
            }
    
            public int iterate(int state, int i)
            {
                return this.table[(state ^ i) & 0xff] ^ (state >>> 8);
            }
            @Override public int hash(int n) {
                int h = -1;
                h = iterate(h, n >>> 24); 
                h = iterate(h, n >>> 16); 
                h = iterate(h, n >>> 8); 
                h = iterate(h, n); 
                return h ^ -1;
            }
            @Override public String toString() { return getClass().getSimpleName(); } 
        }
    
        static class TrigHasher implements Hasher
        {       
            @Override public String toString() { return getClass().getSimpleName(); }
            @Override public int hash(int n) { 
                double s = Math.sin(n);
                return (int) Math.floor((1<<31)*s);
            }       
        }
    
        private static void test(Hasher hasher) {
            System.out.println(hasher+":");
            for (int i = 0; i < 64; ++i)
            {
                int h = hasher.hash(i);
                System.out.println(String.format("%08x -> %08x   %%2 = %d", 
                        i,h,(h&1)));
            }
            for (int i = 0; i < 256; ++i)
            {
                System.out.print(hasher.hash(i) & 1);
            }
            System.out.println();       
            analyzeBits(hasher);
        }
    
        private static void analyzeBits(Hasher hasher) {
            final int N = 65536;
            final int maxrunlength=32;
            int[][] runs = {new int[maxrunlength], new int[maxrunlength]};
            int[] count = new int[2];
            int prev = -1;
            System.out.println("Run length test of "+N+" bits");
            for (int i = 0; i < maxrunlength; ++i)
            {
                runs[0][i] = 0;
                runs[1][i] = 0;
            }
            int runlength_minus1 = 0;
            for (int i = 0; i < N; ++i)
            {
                int b = hasher.hash(i) & 0x1;
                count[b]++;
                if (b == prev)
                    ++runlength_minus1;
                else if (i > 0)
                {
                    ++runs[prev][runlength_minus1];
                    runlength_minus1 = 0;
                }
                prev = b;
            }
            ++runs[prev][runlength_minus1];
    
            System.out.println(String.format("%d zeros, %d ones", count[0], count[1]));
            for (int i = 0; i < maxrunlength; ++i)
            {
                System.out.println(String.format("%d runs of %d zeros, %d runs of %d ones", runs[0][i], i+1, runs[1][i], i+1));         
            }
        }
    
        public static void main(String[] args) {
            Hasher[] hashers = {
                new MultiplicativeHasher1(), 
                GF32Hasher.CRC32, 
                new QuickHasher(),
                new CRC32TableHasher(),
                new TrigHasher()
            };
            for (Hasher hasher : hashers)
            {
                test(hasher);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some (small amount) of data that I'll need quick access to on
I need to write a quick bash script that asks the user which mac
Is there a way to generate a hash of a string so that the
Here is the situation: I have a huge data set that I need quick
I need a quick sanity check. I'm trying to design my views such that
I am in need of a performance-oriented hash function implementation in C++ for a
In Haskell, when I need a quick worker function or helper value, I usually
i need a quick hint regarding the following exercise question: Write a program that
need a quick help here. I have a series of hyperlinks all with the
I need a quick hand figuring out what this code is doing, and how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.