It's free to join Gamasutra!|Have a question? Want to know who runs this site? Here you go.|Targeting the game development market with your product or service? Get info on advertising here.||For altering your contact information or changing email subscription preferences.
Registered members can log in here.Back to the home page.

Search articles, jobs, buyers guide, and more.

By Guy W. Lecky-Thompson
Gamasutra
September 17, 1999

Letters to the Editor:
Write a letter
View all letters


Features

 

Contents

Introduction

Predictably Random Numbers

Name Generation Techniques

Generating Random Terrain

Plot Sequencing

Code Listings

Listing 1

Listing 2

Listing 3

Listing 4

Listing 5

Listing 5:
Namegen.cpp

// 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);

}


 

[Back to] Introduction


join | contact us | advertise | write | my profile
news | features | companies | jobs | resumes | education | product guide | projects | store



Copyright © 2003 CMP Media LLC

privacy policy
| terms of service