/* * Compute entropy value for an array of bytes. * * The returned number represents entropy per bit! * For good long number seed (8bytes seed) it should be in range <2.75,3> (higher is better) * * For large set of bytes (>100) it should be almost 8 (means almost 8 random bits per byte). */ public static float entropy(byte[] f) { int counts[] = new int[256]; float entropy = 0; float total = f.length; for (byte b : f) counts[b+128]++; for (int c : counts) { if (c == 0) continue; float p = c / total; /* Compute entropy per bit in byte. * * To compute entropy per byte compute log with base 256 = log(p)/log(256). */ entropy -= p * Math.log(p)/Math.log(2); } return entropy; }
public static double lnF(double what) { return (what < 1e-06) ? 0 : what * Math.log(what); }