❔ Unique Integer ID GEN from string input
Hi there, I've been trying to think of a unique integer ID generator but let's just say I'm not too good at this
The one I made is quite simple and it just shifts all the characters by 67 (so that all int values of the characters (33 to 126) are in range of 100 to 193) and adds the numbers together (side by side, so 102 '+' 192 => 102192)
It works and the ID's are unique but the issue is quite obvious -> the ID's are way too long.
I could ignore the last 4 characters in my initial range (which are
{ | } ~
) and I would be able to squeeze my range into 10 to 99, but still: for ulong that would mean a character length limit of 9... which is quite low.
I searched online for some unique integer ID generating algorithms but couldn't find anything useful for my case, or I'm just blind and didn't notice something that actually is useful.
Any help in form of advice or links is appreciated.13 Replies
how big or small can your strings get?
The string input will be a name given to a file in my game so I guess there is no limit to how big these can get, depends on the situation, but most likely not bigger than 32 characters I suppose (even if they are bigger I can just reduce them to 32 chars, with this length there shouldn't be any problems)
https://math.stackexchange.com/questions/1684783/how-can-i-convert-this-unique-string-of-characters-into-a-unique-number this one seems promising
Mathematics Stack Exchange
How can I convert this unique string of characters into a unique nu...
I have an unusual programming problem and the math side of it has me stumped. It's probably a simple answer but math isn't my strongest area.
I've generated a unique string of 7 characters which are
Thanks, I'll take a look
For ulong limit
18_446_744_073_709_551_615
Testing this method using highest-value character z
(int 35) we get a limit of 12 characters, any higher that that and there's an overflow...
It's better than my previous 9 character limit but still far away from 32 character limit...
Here's the current implementation
I could possibly try and reduce the possible characters by also ignoring numbers / or not adding 10 to the letters resulting in a range of 0 to 25 instead of 0 to 35.
Numbers are rarely used in my file names anyways so there is little to no risk in there being any collisions with this I suppose.
Now there's a 13 character limit...
Any progress is good.
13 characters is quite a lot to work with already.
I can divide it in 'half' => 6 and 7 characters
Take 6 characters from the beginning of the input
And 7 characters from the end of the input
Unless the input length is < 13, then I just take every character.
That would probably work out fine enough?
Should probably work fine as long as nobody uses names such as "hhhhhhhhhh" and "hhhhhhhhhhhhhhh" which will then probably generate the same ID
Here's the final implementation.
If anyone has any ideas on how to improve this further then feel free to ping methat looks like a really inefficient method, using queues 😬
What would you recommend as an alternative?
Would an array, where -1 is to be skipped, be a better alternative?
You're taking the first 13 and last 7 chars of a string, right?
-1 would be placed during the first loop with the switch in places of characters which aren't A-Z/a-z/0-9 alongside with an incrementing length variable for the ones which are
first 6 and last 7
and some are ignored
@#$_&-+()/ etc
How about this one?
Now there are no queues, only the initial char array
It skips the incorrect characters (
-
), adds the first 6 to the ID, if 6 are already added then it skips through all the other correct chars until there are only 7 leftThat'll be much more performant!
and idk, maybe an additional check in the loop if there are any correct characters left?
There's a possibility that someone will for some reason name a file "
Abc######
" so the loop would continue for 6 more times for no reason, but idk if it makes too much of a differenceMeasure and you'll find out :p
Well, anyways, thanks for the help!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.