I'm trying to make a seeded terrain generator. If anybody has a method of doing this without randomizing, please answer. I don't care how unconventional. :P
The string can be of unlimited length...
local letters = "abcdefghijklmnopqrstuvwxyz" local seed = "whatever"; local sequence = "" local lock = 10; for i=1, string.len(seed) do sequence = sequence..string.find(letters, string.sub(seed, i, i)) end if string.len(sequence)<lock then sequence = sequence..string.rep("0", lock-string.len(sequence)); elseif string.len(sequence)>lock then sequence = string.sub(sequence, 1, lock); end print(sequence);
Something like that.
This is called "hashing."
Here's an implementation of Java's hashCode
method on strings
function hashCode(str) local sum = 0; local n = #str; for i = 1,#str do local k = str:sub(i,i):byte(); sum = (sum + k * 31 ^ (n - 1 - (i - 1))) %( 2 ^ 31); end return sum; end
This produces a number in the range of 0 to 2^31 - 1.
To get a 10 digit number from this (maximally), we would take it mod 10^10:
hashCode(str) % (10 ^ 10)