Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I encode a string into a 10 digit number?

Asked by
Noculus 25
10 years ago

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...

0
String into a sequence of 10 numbers as in? PiggyJingles 358 — 10y
0
a ten digit number sorry... Noculus 25 — 10y
0
Give me a moment, taking longer since I am on my phone. PiggyJingles 358 — 10y

2 answers

Log in to vote
0
Answered by 10 years ago
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.

0
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. User#2 0 — 10y
0
Ok, I think that will work partially :3 I might just add a couple irrational numbers to it.... :P Noculus 25 — 10y
0
Oops, I'm sorry. I meant string.find(letters...) not string.find(seed...) PiggyJingles 358 — 10y
0
Added a lock to 10 chars. PiggyJingles 358 — 10y
Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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)

Answer this question