Edit: Removed mentioning of hashCode()
I'm working on a seed-based terrain generator so that if a user enters a specific String into the text field, it will produce the same world every time. The way I was planning to do this was to use an algorithm that produces a 19-digit-long long (pun intended). The program will then utilize value of certain indices, or a combination of values of certain indicies, and other computations to set certain aspects of the world, like biomes, amplitude, and variance. In essence, whenever the same seed is inputted, the same internal 19-digit long will be produced by the algorithm, and by consequence, the same world.
How can I create a custom hashing method that produces a 19-digit long for every input value? Before, I was using this:
private void generate() {
long[] seedBuilder = new long[5]; // So I can print certain parts of the process for debugging purposes
String clientSeed = seedField.getText(); // The input aka seed
if (clientSeed.equals("")) clientSeed = String.valueOf(System.currentTimeMillis()); // use time if none is given
// Basic hash of seed
seedBuilder[0] = (long)clientSeed.hashCode();
// primary-char hash * concluding-char hash (for reducing collisions)
long comboID = clientSeed.substring(0,1).hashCode() * clientSeed.substring(clientSeed.length()-1).hashCode();
// Basic hash * comboID
seedBuilder[1] = seedBuilder[0] * comboID;
// Absolute value
seedBuilder[2] = Math.abs(seedBuilder[1]);
// Raised to the 1.27
seedBuilder[3] = (long)(Math.pow(seedBuilder[2], 1.27));
// Multiplied by 10.1 until digits = 19
seedBuilder[4] = (long)(Math.pow(9.9,(19-(seedBuilder[3]+"").length())) * seedBuilder[3]);
long generator = seedBuilder[4]
}
Basically, I got this through a lot of experimentation. The power of 1.27 was chosen arbitrarily and the 9.9 was just because anything over 10.1 could produce Long.MAX_VALUE, but I still wanted to get 19 digits. The problem is that some brute-force random-string testing showed that the distribution is really uneven; 60% of the results (aka seedBuilder[4]) began with 3 or 4, and 6 or 7 appear as the first index value like 5% of the time, I believe I recall correctly. Whatever aspects of the world that would be controlled by the first index would be the same a lot of the time.
I want to do this in a less arbitrary, and more professional and safe way to produce a 19-digit long I can use for generation. Thanks. Please ask me to clarify something if it is unclear.