|
-
October 17th, 2010, 05:06 PM
#1
function that takes a string and returns a percent value
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.
-
October 18th, 2010, 05:14 AM
#2
Re: function that takes a string and returns a percent value
Look into hash functions. Most modern programming languages can generate a hash for a string. You can rescale that to a percentage value.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 18th, 2010, 06:32 PM
#3
Re: function that takes a string and returns a percent value
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?
-
October 19th, 2010, 05:20 AM
#4
Re: function that takes a string and returns a percent value
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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
October 20th, 2010, 04:52 AM
#5
Re: function that takes a string and returns a percent value
 Originally Posted by D_Drmmr
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),
Code:
#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.
Last edited by nuzzle; October 20th, 2010 at 11:29 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|