Génération de nom aléatoire
Publié : sam. 09/mai/2009 8:45
Je cherche depuis un bout de temps un code qui génère un nom aléatoirement pour un petit jeu que je suis en train de faire. J'ai adapté un code que j'ai trouvé ...
voilà ce que ça donne....
le code a été adapté rapidement, mais ça peut être améliorer... ici toute les variables sont global on peu améliorer ça !
Le resultat est moyen mais ça marche. Si vous connaissez de meilleur code, ou si vous avez une idée pour améliorer ça ! n'hésitez pas !
voilà ce que ça donne....
le code a été adapté rapidement, mais ça peut être améliorer... ici toute les variables sont global on peu améliorer ça !
Le resultat est moyen mais ça marche. Si vous connaissez de meilleur code, ou si vous avez une idée pour améliorer ça ! n'hésitez pas !
Code : Tout sélectionner
;Source:http://www.uselesspython.com/download.php?script_id=32
;Init fricatives
Restore fricatives
Global Dim fricatives.s(1);
n.l=0;
While fricatives(n)<>"*"
n+1
ReDim fricatives.s(n)
Read.s fricatives(n)
Wend
;Init vowels
Restore vowels
Global Dim vowels.s(1);
n.l=0;
While vowels(n)<>"*"
n+1
ReDim vowels.s(n)
Read.s vowels(n)
Wend
;Init consonantsNormal
Restore consonantsNormal
Global Dim consonantsNormal.s(1);
n.l=0;
While consonantsNormal(n)<>"*"
n+1
ReDim consonantsNormal.s(n)
Read.s consonantsNormal(n)
Wend
Restore consonantsNasal
Global Dim consonantsNasal.s(1);
n.l=0;
While consonantsNasal(n)<>"*"
n+1
ReDim consonantsNasal.s(n)
Read.s consonantsNasal(n)
Wend
Global result.s, vowelSyllables.l, randomLength.l, vowelRatio.l,syllableOnlyVowels.l
Declare maincongen()
Declare sylgen()
Declare fricgen()
Declare vowgen()
Declare congen()
Declare con2gen()
Declare con3gen()
Procedure.s namegen(ratio.l=5, lower_limit.l=3, upper_limit.l=11)
ratio = ratio + 3
If ratio < 4:
ratio = 4
ElseIf ratio > 14:
ratio = 14
EndIf
vowelRatio = ratio
result = ""
vowelSyllables = 0
randomLength = Random(upper_limit-lower_limit)+lower_limit
sylgen()
ProcedureReturn result
EndProcedure
Procedure sylgen()
syllableOnlyVowels = 1
maincongen()
If vowelSyllables < (vowelRatio/4):
vowgen()
EndIf
maincongen()
If syllableOnlyVowels = 1:
vowelSyllables = vowelSyllables + 1
EndIf
If Len(result) < randomLength:
sylgen()
EndIf
EndProcedure
Procedure fricgen()
result = result + fricatives(Random(ArraySize(fricatives())-1))
syllableOnlyVowels = 0
vowelSyllables = 0
EndProcedure
Procedure vowgen()
result = result + vowels(Random(ArraySize(vowels())-1))
EndProcedure
Procedure congen()
result = result + consonantsNormal(Random(ArraySize(consonantsNormal())-1))
syllableOnlyVowels = 0
vowelSyllables = 0
EndProcedure
Procedure con2gen()
If Random(2) = 0:
result = result + "r"
Else:
result = result + "l"
EndIf
syllableOnlyVowels = 0
vowelSyllables = 0
EndProcedure
Procedure con3gen():
result = result + consonantsNasal(Random(ArraySize(consonantsNasal())-1))
syllableOnlyVowels = 0
vowelSyllables = 0
EndProcedure
Procedure maincongen()
If Len(result) < randomLength:
randomNumber = Random(vowelRatio)
If randomNumber = 0:
fricgen()
If Len(result) < randomLength:
randomNumber = Random(vowelRatio/4 * 3)
If randomNumber = 0:
congen()
If Len(result) < randomLength:
randomNumber = Random(vowelRatio/2)
If randomNumber = 0:
con2gen()
EndIf
EndIf
ElseIf randomNumber = 1:
con2gen()
ElseIf randomNumber = 2:
con3gen()
EndIf
EndIf
ElseIf randomNumber = 1:
congen()
If Len(result) < randomLength:
randomNumber = Random(vowelRatio/2)
If randomNumber = 0:
con2gen()
EndIf
EndIf
ElseIf randomNumber = 2:
con2gen()
ElseIf randomNumber = 3:
con3gen()
EndIf
EndIf
EndProcedure
For z=1 To 10
Debug namegen(Random(10))
Next
DataSection
fricatives:
Data.s "j", "ch", "h", "s", "sh", "th", "f", "v", "z","*"
vowels:
Data.s "a", "e", "i", "o", "u", "y", "ya", "ye", "yi", "yo", "yu", "wa", "we", "wi", "wo", "wu", "ae", "au", "ei", "ie", "io", "iu", "ou", "uo", "oi", "oe", "ea","*"
consonantsNormal:
Data.s "c", "g", "t", "d", "p", "b", "x", "k", "ck", "ch","*"
consonantsNasal:
Data.s "n", "m", "ng", "nc","*"
EndDataSection