Page 1 sur 1

Génération de nom aléatoire

Publié : sam. 09/mai/2009 8:45
par Thyphoon
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 !

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

Publié : sam. 09/mai/2009 10:29
par Backup
.................

Publié : sam. 09/mai/2009 10:35
par Thyphoon
c'est pas mal du tout dobro !! :P

Publié : sam. 09/mai/2009 10:39
par Backup
Merci :)

je viens juste d'apporter une petite modification

pour ce qui concerne la longueur des noms a trouver ;)

Publié : sam. 09/mai/2009 10:47
par jbernard13
Et pour generer un nombre aléatoire ?

Publié : sam. 09/mai/2009 10:54
par Thyphoon
jbernard13 a écrit :Et pour generer un nombre aléatoire ?

Code : Tout sélectionner

debug random(1000)

Publié : lun. 11/mai/2009 21:46
par lionel_om
DoBro, pourquoi tu n'utilise pas 'a' au lieu de ta constante 65 et ainsi de suite...? Ca serait plus clair et plus rapide aussi pour toi à écrire...

/Lio

Publié : lun. 11/mai/2009 21:58
par Backup
lionel_om a écrit :DoBro, pourquoi tu n'utilise pas 'a' au lieu de ta constante 65 et ainsi de suite...? Ca serait plus clair et plus rapide aussi pour toi à écrire...

/Lio
bah , c'est le resultat qui compte :lol:

mais tu as raison, j'ai fait ça en 5 minutes, alors j'ai programmé avec mes doigts , sans trop réfléchir , ce qui parfois ne m'amène pas forcement
la simplicité ! :)