Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Checksum?

Templar Baphomet
Man in Black
Join date: 13 Sep 2005
Posts: 135
10-02-2005 15:45
Kind of embarrassing, but could someone please look this over and see where I'm going wrong or point to a better code? I want to generate an integer number to represent an object key that has a better chance of being unique in a given area than using llFrand. The range of outputs on this is only like 9,000 values, so that doesn't seem right, but I've tested a number of things (substitution and exchanges, etc.) and not been able to generate duplicate checksums.


integer KeyChecksum(string object_key) {
integer running_total = 0;
integer string_length = 0;
integer i;
string char;
string char_mapper = "0123456789abcdef-";

string_length = llStringLength(object_key);
if (string_length <= 0) {
return(-1);
}
else {
for (i = 0; i < string_length; i++) {
char = llGetSubString(object_key, i, i);
running_total += (i + 1) * (llSubStringIndex(char_mapper, char) + 1);
}
return(running_total);
}
}
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
10-02-2005 20:18
CODE

integer KeyChecksum(string object_key) {
integer running_total = 0;
integer string_length = 0;
integer i;
string char;
string char_mapper = "0123456789abcdef-";

string_length = llStringLength(object_key);
if (string_length <= 0) {
return(-1);
}
else {
for (i = 0; i < string_length; i++) {
char = llGetSubString(object_key, i, i);
running_total += (i + 1) * (llSubStringIndex(char_mapper, char) + 1);
}
return(running_total);
}
}


For ease of use lets define a key as 4 32bit integers, A, B, C, D
AAAAAAAA-BBBB-BBBB-CCCC-CCCCDDDDDDDD
Depends exactly what you had in mind for the data spread.
if we expand this we get
CODE

((A >> 28) & 0xF) * 1 +
((A >> 24) & 0xF) * 2 +
((A >> 20) & 0xF) * 3 +
((A >> 16) & 0xF) * 4 +
((A >> 12) & 0xF) * 5 +
((A >> 8) & 0xF) * 6 +
((A >> 4) & 0xF) * 7 +
(A & 0xF) * 8 +
16 * 9 +
((B >> 28) & 0xF) * 10 +
((B >> 24) & 0xF) * 12 +
((B >> 20) & 0xF) * 13 +
((B >> 16) & 0xF) * 14 +
16 * 15 +
((B >> 12) & 0xF) * 16 +
((B >> 8) & 0xF) * 17 +
((B >> 4) & 0xF) * 18 +
(B & 0xF) * 19 +
16 * 20 +
((C >> 28) & 0xF) * 21 +
((C >> 24) & 0xF) * 22 +
((C >> 20) & 0xF) * 23 +
((C >> 16) & 0xF) * 24 +
16 *25 +
((C >> 12) & 0xF) * 26 +
((C >> 8) & 0xF) * 27 +
((C >> 4) & 0xF) * 28 +
(C & 0xF) * 29 +
((D >> 28) & 0xF) * 30 +
((D >> 24) & 0xF) * 31 +
((D >> 20) & 0xF) * 32 +
((D >> 16) & 0xF) * 33 +
((D >> 12) & 0xF) * 34 +
((D >> 8) & 0xF) * 35 +
((D >> 4) & 0xF) * 36 +
(D & 0xF) * 37


The range of the output is [1104,10059].

You should take a look at
LSLWiki:ExampleKey2Channel
LSLWiki:LibraryPseudoRandomGenerator
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Templar Baphomet
Man in Black
Join date: 13 Sep 2005
Posts: 135
10-09-2005 16:42
Forgot to say, "Thanks!" The Key2Channel approach is just the ticket.