This is mostly a joke project to test an algorithm for Markov Chains that can be used to create random bits and bobs. In this case you can use it to create random names for example.
You give the program a number of samples and it will base the random generation of these. I'm using numbers here, but I did try with characters from the book The Belgariad and I did get it to generate a name from the book that wasn't in the samples list.
The property minLength is the shortest name it can generate and the property order is the number of characters in the string it will look at. Don't set it to a number lower than 1.
I'm not sure when you would want to use this, maybe if you are making a RPG you could create new names every time you start the game. That could be fun.
SetWindowTitle( "Name Generator (Markov Chain Test)" )
SetWindowSize( 1024, 768, 0 )
SetPrintSize(2)
Type Link
Token$
Letters$ As String[]
EndType
Global samples$ As String[]
samples$.insert("one")
samples$.insert("two")
samples$.insert("three")
samples$.insert("four")
samples$.insert("five")
samples$.insert("six")
samples$.insert("seven")
samples$.insert("eight")
samples$.insert("nine")
samples$.insert("ten")
samples$.insert("eleven")
samples$.insert("twelve")
samples$.insert("thirteen")
samples$.insert("fourteen")
samples$.insert("fifteen")
samples$.insert("sixteen")
samples$.insert("seventeen")
samples$.insert("eighteen")
samples$.insert("nineteen")
samples$.insert("twenty")
Global chain As Link[]
REM Make sure all the samples are in uppercase
For i = 0 To samples$.Length
samples$[i] = Upper(samples$[i])
Next i
REM Set some properties regarding the chains
Global order = 2
Global minLength = 3
BuildChain()
name$ = NextName()
Repeat
print("Name: " + name$)
If GetRawKeyState(32) = 1
For l = 0 To chain.Length
Print(chain[l].Token$)
For ll = 0 To chain[l].Letters$.Length
Print(" " + chain[l].Letters$[ll])
Next ll
Next l
EndIf
If GetRawKeyPressed(116) = 1 Then name$ = NextName()
Sync()
Until GetRawKeyPressed(27) = 1
REM Build chains
Function BuildChain()
For name = 0 To samples$.Length
_len = Len(samples$[name]) - order
For letter = 1 To _len
token$ = Mid(samples$[name], letter, order)
link As Link
link = GetLink(token$)
link.Letters$.Insert(Mid(samples$[name], letter + order, 1))
SaveLink(link)
Next letter
Next name
EndFunction
Function GetLink(token$)
link As Link
idx = chain.Find(token$)
If idx < 0
link.Token$ = token$
chain.InsertSorted(link)
Else
link = chain[idx]
EndIf
EndFunction link
Function SaveLink(link As Link)
idx = chain.Find(link.Token$)
chain[idx] = link
EndFunction
Function NextName()
retval$ = ""
While Len(retval$) < minLength
n = Random(0, samples$.Length)
nameLength = Len(samples$[n])
retval$ = Mid(samples$[n], Random(1, nameLength - order), order)
While Len(retval$) < nameLength
token$ = Right(retval$, order)
letter$ = GetLetter(token$)
If letter$ <> "?"
retval$ = retval$ + letter$
Else
Exit
EndIf
EndWhile
EndWhile
EndFunction retval$
REM Get's a random letter from the chain
Function GetLetter(token$)
retval$ = ""
idx = chain.Find(token$)
If idx = -1
retval$ = "?"
Else
n = Random(0, chain[idx].Letters$.Length)
retval$ = chain[idx].Letters$[n]
EndIf
EndFunction retval$