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...
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 will give you "1234" for "bleh", but will also give "1234" for any 4 letter combination. Not a very good seeding choice (nor does it force the "number" to be 10 digit longs) but it works.
Ok, I think that will work partially :3 I might just add a couple irrational numbers to it.... :P
Oops, I'm sorry. I meant string.find(letters...) not string.find(seed...)
Added a lock to 10 chars.
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)
This discussion is preserved as it was. New answers, comments, and votes are closed.
3 comments
String into a sequence of 10 numbers as in?
a ten digit number sorry...
Give me a moment, taking longer since I am on my phone.