Letter generator, according to commonly used letters
Posted: Tue Jul 29, 2008 11:51 am
Hi!
So I've currently got a chunk of code I'm working with that generates random letters to fill in extra spaces in a word search puzzle grid. Right now it's just this bit sitting inside a few loops for the random letter generator:
$randletter = chr(ord("a") + rand(0, 25));
Everything works fine as-is. However, I got a suggestion that there were perhaps too many Xs and Zs and uncommon letters in general, and not enough of rather common letters such as vowels. So then I started wondering if it would be possible to make a random (err, not so random in this case) letters function that spit out letters according to how common they are in the English language. Possibly using a multidimensional array to store each letter, plus a percentage value based on how common it is, plus a counter that increases each time that letter is used? And then somehow use the percentage plus the code snippet above to reject generated letters whose percentages are lower the other letters, until a higher percentage letter is generated, and use that one instead? Problem is, seems like there'd need to be a rather high number of letters generated before 26 separate percentages could be really practical to have, and while this program could be generating up to several hundred letters, that's still not all that many, considering. Or, maybe the percentages might need to be generalized and done away with, and I could somehow just have low-demand, mid-demand, and high-demand categories of letters? Or something along those lines?
So, this is more of a 'can this be done in a non-painful way' question then any sort of 'asking for help to write any hard code'. First off, I'm not sure if something like this might already exist out there? Second, I'm having a hard time wrapping my wee brain around what sort of logic needs to be used here, as well. So, I'd appreciate any useful thoughts that could be thrown at the matter.
Thanks!
So I've currently got a chunk of code I'm working with that generates random letters to fill in extra spaces in a word search puzzle grid. Right now it's just this bit sitting inside a few loops for the random letter generator:
$randletter = chr(ord("a") + rand(0, 25));
Everything works fine as-is. However, I got a suggestion that there were perhaps too many Xs and Zs and uncommon letters in general, and not enough of rather common letters such as vowels. So then I started wondering if it would be possible to make a random (err, not so random in this case) letters function that spit out letters according to how common they are in the English language. Possibly using a multidimensional array to store each letter, plus a percentage value based on how common it is, plus a counter that increases each time that letter is used? And then somehow use the percentage plus the code snippet above to reject generated letters whose percentages are lower the other letters, until a higher percentage letter is generated, and use that one instead? Problem is, seems like there'd need to be a rather high number of letters generated before 26 separate percentages could be really practical to have, and while this program could be generating up to several hundred letters, that's still not all that many, considering. Or, maybe the percentages might need to be generalized and done away with, and I could somehow just have low-demand, mid-demand, and high-demand categories of letters? Or something along those lines?
So, this is more of a 'can this be done in a non-painful way' question then any sort of 'asking for help to write any hard code'. First off, I'm not sure if something like this might already exist out there? Second, I'm having a hard time wrapping my wee brain around what sort of logic needs to be used here, as well. So, I'd appreciate any useful thoughts that could be thrown at the matter.
Thanks!