ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm looking for a function to map a string to a number in the range [0,MAX]. What the number actually is doesn't really matter, but it should be repeatable for a given input (not random).
I've had a look around and can't find anything on this. I could write a function myself but I know it would be horribly inefficient and there's probably a perfect solution floating around somewhere...
I'm looking for a function to map a string to a number in the range [0,MAX]. What the number actually is doesn't really matter, but it should be repeatable for a given input (not random).
If the number really doesn't matter, then this should work.
Code:
function map_to_int($some_string) { return 1; }
But seriously, how about some more information. Maybe tell us what it is you're trying to accomplish? What your range of ints is? What kind of strings you're going to be getting? It's hard to give a useful answer without any sort of context.
A har har har, didn't know we had a comedian in the house
I have an array of words or phrases, and given an input word (string), I need to associate it with one of the elements in the array (pseudo-randomly, but a given input should always produce the same result). So if the array has a size MAX, then I need to produce numbers in the range [0, MAX-1].
For the size of MAX, say, 100.
I basically need a fast hash function that produces an integer in a given range.
Actually, that won't work. The md5() function returns a string, not an integer. Granted, it's a hexidecimal string, but when you do the modulus, PHP casts it to an int, which truncates everything after the first non-digit character. So any string for which the MD5 sum starts with a letter will get mapped to 0.
Edit:
Actually, on second thought, that may be close enough for current purposes, as it will certainly generate consistent values. It's just that the results won't be consistent with the actual numeric value of the MD5 sum. But that may not be a problem in this case.
A har har har, didn't know we had a comedian in the house
Yeah, sorry, but I have a penchant for bad jokes and you left yourself wide open on that one.
Quote:
I basically need a fast hash function that produces an integer in a given range.
Well, I'm still not real clear on what you're actually trying to accomplish, but at least now we've got some context.
The easiest solution is probably the approach jlinkels suggested -using a pre-defined hash function and a modulus. The only problem is that you need a hash function that will generate an integer - at 128-bits and 160-bits respectively, both MD5 and SHA1 generate values far to large to fit into an integer data type. Your best bet is probably CRC32, as it actually generates a 32-bit integer value. You'll just have to convert the return value to an unsigned int as described in the documentation.
Your best bet is probably CRC32, as it actually generates a 32-bit integer value. You'll just have to convert the return value to an unsigned int as described in the documentation.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.