Dorkinator 64 Share Posted June 17, 2017 The purpose of this is to allow you to have temporal control over RNG without saving data between sessions. For example if you want to randomize the loop speed of a script between 2 numbers and you want variance between people this would be perfect, or if you want a configuration setting to be random for the users of your script you can use this. package random; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * Created by Dorkinator on 6/17/2017. */ public class RandomConstant { private String seed; private String constant; private long duration = 0; private int min; private int max; public RandomConstant(String seed, String constant, int min, int max){ this.seed = seed; this.constant = constant; this.min = min; this.max = max; } public RandomConstant(String seed, String constant, int min, int max, long duration){ this.seed = seed; this.constant = constant; this.min = min; this.max = max; this.duration = duration; } private long timeOffset(){ return (long)(nextDouble(seed+constant+min+max+(System.currentTimeMillis()/duration))*duration); } private double nextDouble(String input){ String hash = hash(input); final int ln = 6; final double max = Math.pow(16,ln); return ((double)Integer.parseInt(hash.substring(hash.length()-ln,hash.length()), 16))/max; } private double nextDouble(){ return nextDouble(seed + constant + min + max + ((System.currentTimeMillis()+timeOffset()) / duration)); } public int get(){ return (int)((nextDouble()*(double)(max-min))+(double)min); } private static String hash(String input){ MessageDigest md; try { md = MessageDigest.getInstance("SHA-512"); md.update((input).toString().getBytes("UTF-8")); // Change this to "UTF-16" if needed byte[] digest = md.digest(); return String.format("%064x", new java.math.BigInteger(1, digest)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } } Link to comment Share on other sites More sharing options...
distraction 61 Share Posted June 18, 2017 Thank you for the snippet, and also for taking time to answer some questions I had in discord! I really appreciate that. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.