Jump to content

function that takes a string and returns a percent value

- - - - -

  • Please log in to reply
7 replies to this topic

#1
mdbr

mdbr

    Newbie

  • Members
  • Pip
  • 4 posts
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.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Sounds like converting a hash value to a percentage.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
mdbr

mdbr

    Newbie

  • Members
  • Pip
  • 4 posts
Yeah, that would be perfect, but I actually haven't got a clue on how to do that :)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
mdbr

mdbr

    Newbie

  • Members
  • Pip
  • 4 posts
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
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
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
[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.

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

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.
Consider three strings:
"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).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
mdbr

mdbr

    Newbie

  • Members
  • Pip
  • 4 posts
Thanks, both ways are pretty awesome :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users