Click to See Complete Forum and Search --> : function that takes a string and returns a percent value
mdbr
October 17th, 2010, 05:06 PM
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.
D_Drmmr
October 18th, 2010, 05:14 AM
Look into hash functions. Most modern programming languages can generate a hash for a string. You can rescale that to a percentage value.
mdbr
October 18th, 2010, 06:32 PM
I don't know why I didn't think of that right away, actually... But what would be the easiest way of getting percentage values from hashes so that they would be uniformly distributed from 0 to 100?
D_Drmmr
October 19th, 2010, 05:20 AM
That depends on the data type in which you need the percentage. If you need integer data, then just use modulus. If you need floating point data, then you'll have define what "uniformly distributed" means.
nuzzle
October 20th, 2010, 04:52 AM
That depends on the data type in which you need the percentage. If you need integer data, then just use modulus. If you need floating point data, then you'll have define what "uniformly distributed" means.
Why does "uniformly distributed" needs to be defined in the floating point case but not in the int case? The term is used all the time in association with random number generation both of ints and of floating points.
In your suggestion you in principle use a linear congruent random number generator (but with a suboptimal choise of parameters). You plug in the hash value as seed and draw one random int in the [1..100] interval. The same can be done to generate a floating point. Here's an example using VS 2010 Express (making use of tr1 facilities of C++0x),
#include <random>
#include <functional>
double stringToPercent(std::string s) {
std::hash<std::string> hash_function; // the standard hash function for strings
size_t hash_value = hash_function(s); // hash the string
std::uniform_real_distribution<double> distribution(0.0,100.0); // uniform distribution of doubles in range 0.0 to (but not including) 100.0
std::minstd_rand0 engine(hash_value); // linear congruent random number generating engine seeded with hash value
auto generator = std::bind(distribution, engine); // connect distribution with engine
generator(); // waste one random number
double percent = generator(); // use the next
return percent;
}
Note that the first random double is wasted. This is to allow the chaotic feature of the random engine formula to further disperse similar strings.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.