Hello everybody,
I'm not that good at math, but I need a function that calculates some percent value for a given set of characters. The value doesn't have to represent anything at all, but for every string the function has to return a certain value every time. Also, the value shouldn't be affected in a direct way by the number of characters in the string or the characters themselves i.e. there is no linear dependency between the length of a string/the characters in it and the percent value for it. In a way this is similar to generating a random percent number, but the value has to be the same each time it is calculated for a certain string.
Any help would be greatly appreciated.
7 replies to this topic
#1
Posted 17 October 2010 - 01:54 PM
|
|
|
#2
Posted 17 October 2010 - 02:33 PM
Sounds like converting a hash value to a percentage.
#3
Posted 17 October 2010 - 03:01 PM
Yeah, that would be perfect, but I actually haven't got a clue on how to do that :)
#4
Posted 17 October 2010 - 04:17 PM
Well, the algorithm for MD5 hashes is pretty well known. Taking the resulting hex code, you could take the first few characters as as a percentage.
#5
Posted 17 October 2010 - 04:52 PM
But how would I do that? I could count each letter and each digit character as a certain percent value, but I'm afraid the resulting values for different strings won't be gradually distributed from 0 to 100%, they will always be closer to 0 or 100% depending on the way that I count each character.
#6
Posted 17 October 2010 - 09:30 PM
I would add each hexadecimal representation of the hash together as WingedPanther suggested (I think?), say E1FEAAB3.. => E1+FE+AA+B3.., then apply modulo 100 - 1, let me calculate distributions of that on random MD5s
If you cannot implement this into your project, you can get the hexadecimal representation of the ASCII of each letter or number that you are given, and apply the same method, shouldn't be too different.
[SIZE=1]Percent => Times 0% => int 1017 1% => int 984 2% => int 991 3% => int 964 [..similar results..] 97% => int 976 98% => int 976 99% => int 1022[/SIZE][SIZE=1] 100% => int 989[/SIZE]Fairly uniform (deviation = 3.13%) as those hashes have a fair probability of the "landslide" effect even on small strings.
If you cannot implement this into your project, you can get the hexadecimal representation of the ASCII of each letter or number that you are given, and apply the same method, shouldn't be too different.
Edited by Alexander, 17 October 2010 - 10:14 PM.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#7
Posted 18 October 2010 - 11:38 AM
mdbr said:
But how would I do that? I could count each letter and each digit character as a certain percent value, but I'm afraid the resulting values for different strings won't be gradually distributed from 0 to 100%, they will always be closer to 0 or 100% depending on the way that I count each character.
"Hello world": 3e25960a79dbc69b674cd4ec67a72c62
"HEllo world": 68df723b3e61541ec5af9c6053357942
"HELLO WORLD": 361fadf1c712e812d198c4cab5712a79
Take the first 4 characters, convert to decimal integer, and divide by 65536.0 to get values ranging from 0 (inclusive) to 1 (exclusive).
#8
Posted 19 October 2010 - 10:38 AM
Thanks, both ways are pretty awesome :)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









