| |
|
|
||||
![]() |
||||||
| |
|
|||||
|
Listing
5: // Building a word from a letter frequency table // (c) 1999 Guy W. Lecky-Thompson // All Rights Reserved #include <stdio.h> #include "Prand.h" // Pseudo-randomizer #include <string.h> int GetLetterPosition(unsigned long ulWordTable[28][28], PseudoRandomizer * prGenerator, int nPrevious) { int nCounter; unsigned long ulFrequencyTotal, ulFrequencyRunningTotal, ulRandomLetter; // Get the frequencies for (nCounter = 1; nCounter < 27; nCounter++) { ulFrequencyTotal = ulFrequencyTotal + ulWordTable[nPrevious][nCounter]; }
// Choose a 'target' frequency ulRandomLetter = prGenerator()->PseudoRandom(ulFrequencyTotal); // Move through the table until we hit the 'target' frequency ulFrequencyRunningTotal = 0; nCounter = 1; do { ulFrequencyRunningTotal = ulFrequencyRunningTotal + ulWordTable[nPrevious][nCounter]; nCounter++; } while (ulFrequencyRunningTotal < ulRandomLetter); return nCounter; } void GenerateWord(int nLetters, char * szWord, unsigned long ulWordTable[28][28], PseudoRandomizer * prGenerator) { int nLetterPosition; // First, find the letter that can start the word nLetterPosition = GetLetterPosition(ulWordTable,prGenerator,0); // We have the first letter sprintf(szWord,"%c\0",(nLetterPosition-1) + 'a'); // Repeat until nLetters have been added do { nLetterPosition = GetLetterPosition(ulWordTable,prGenerator,nLetterPosition); sprintf(szWord,"%c\0",(nLetterPosition-1) + 'a'); } while ((int)strlen(szWord) < nLetters); printf("Word : %s\n",szWord); }
|
|
|