Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

3 comments

0

String into a sequence of 10 numbers as in?

0

a ten digit number sorry...

0

Give me a moment, taking longer since I am on my phone.

2 answers

✓ Accepted answer
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.

4 comments

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.

0

Ok, I think that will work partially :3 I might just add a couple irrational numbers to it.... :P

0

Oops, I'm sorry. I meant string.find(letters...) not string.find(seed...)

0

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.