Month: December 2016

  • Run MD5 check sum against all files in a directory

    Couple snippets that allow us to run checksum and get unique md5 checksums.

    This is two step process. First, we obtain our md5 checksum for all files

    find -type f -exec md5sum "{}" + > /opt/checklist.chk
    

    This produces file with following contents

    71cc452a8ac5a27c32a83e6a0909e7ae  ./PID_190_7344_0_47710322.tif6712032974632727465.tiff
    71cc452a8ac5a27c32a83e6a0909e7ae  ./PID_190_7344_0_47710322.tif174464329785828524.tiff
    71cc452a8ac5a27c32a83e6a0909e7ae  ./PID_190_7344_0_47710322.tif6775939766281585264.tiff
    71cc452a8ac5a27c32a83e6a0909e7ae  ./PID_190_7344_0_47710322.tif7205305688614612348.tiff
    71cc452a8ac5a27c32a83e6a0909e7ae  ./PID_190_7344_0_47710322.tif3909999865608008175.tiff
    

    Next we parse and get only unique checksums.

    cat  /opt/checklist.chk | awk '{split($0, a, " "); if(!seen[a[1]]++) print a[1]}'
    

    This produces our distinct checksums

    71cc452a8ac5a27c32a83e6a0909e7ae
    
  • Random number between two values

    This is a small utility class that allows us to obtain a random number between two values that are uniformly distributed in the range of ‘low’ to ‘high’. This works for floats, doubles and integers.

    The inner working of this class are straight forward, our uniform(int, int) method uses the nextInt(int) method of Random class which already allows us to pass the upper bound. Float and Double work by obtaining a value in range [0.0, 1.0] and then scaling it accordingly between our ‘low’ and ‘high’

    As this is meant for use in multithreaded environment I am using java.util.concurrent.ThreadLocalRandom rather than java.util.Random for performance reasons.

    import java.util.concurrent.ThreadLocalRandom;
    
    import java.util.concurrent.ThreadLocalRandom;
    
    public class RandomUtil
    {
        public static int uniform(final int low, final int high)
        {
            final ThreadLocalRandom rand = ThreadLocalRandom.current();
            return rand.nextInt(high - low) + low;
        }
    
        public static float uniform(final float low, final float high)
        {
            final ThreadLocalRandom rand = ThreadLocalRandom.current();
            return rand.nextFloat() * (high - low) + low;
        }
    
        public static double uniform(final double low, final double high)
        {
            final ThreadLocalRandom rand = ThreadLocalRandom.current();
            return rand.nextDouble() * (high - low) + low;
        }
    
        public static double nextDouble()
        {
            final ThreadLocalRandom rand = ThreadLocalRandom.current();
            return rand.nextDouble();
        }
    
        public static boolean nextBoolean()
        {
            final ThreadLocalRandom rand = ThreadLocalRandom.current();
            return rand.nextBoolean();
        }
    }