java - Create a uniform random number based on a hash -
i need pseudo random number based on key consisting of string , long. should same random number when query using same key , also, should different number if query using different key, when long in key off 1. tried code , random numbers unique similar numbers seem correlated.
import java.util.date; import java.util.random; import org.apache.commons.lang3.builder.hashcodebuilder; public class hashkeytest { long time; string str; public hashkeytest(string str, long time) { this.time = time; this.str = str; } @override public int hashcode() { return new hashcodebuilder().append(time).append(str).tohashcode(); } public static void main(string[] args) throws exception { for(int i=0; i<10; i++){ long time = new date().gettime(); hashkeytest hk = new hashkeytest("spy", time); long hashcode = (long)hk.hashcode(); random rgen = new random(hashcode); system.out.format("%d:%d:%10.12f\n", time, hashcode, rgen.nextdouble()); thread.sleep(1); } } }
solution pieced together. works pretty well, wonder if needs verbose.
import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.objectoutputstream; import java.io.serializable; import java.nio.bytebuffer; import java.security.messagedigest; import java.security.nosuchalgorithmexception; import java.util.random; public class hashkeytest implements serializable{ long time; string str; public hashkeytest(string str, long time) { this.time = time; this.str = str; } public double random() throws ioexception, nosuchalgorithmexception { bytearrayoutputstream bos = new bytearrayoutputstream(); objectoutputstream out = new objectoutputstream(bos); out.writeobject(this); byte[] bytes = bos.tobytearray(); messagedigest md5digest = messagedigest.getinstance("md5"); byte[] hash = md5digest.digest(bytes); bytebuffer bb = bytebuffer.wrap(hash); long seed = bb.getlong(); return new random(seed).nextdouble(); } public static void main(string[] args) throws exception { long time = 0; (int = 0; < 10; i++) { time += 250l; hashkeytest hk = new hashkeytest("spy", time); system.out.format("%d:%10.12f\n", time, hk.random()); thread.sleep(1); } } }
you said "i should same random number when query using same key , also, should different number if query using different key". if understand question correctly, not want random number, rather cryptographic hash code.
you should @ passing whatever data have through hash function sha or md5. give seemingly random respect input, same given same input, , vary wildly if input vary little.
edit: consistently obtain double values try (pseudo-code):
shahashvalue v = computesha( yourobject); random r = new random(v); the_random_value = r.getnext();
the idea here use sha hash value seed initialize random generator. pretty have, don't know hashbuilder produces in terms of different values. using sha hashes instead might improve situation.
you should consider "very different" values doubles between 0 , 1 might not apparent.
Comments
Post a Comment