Générer une chaine de caractére

Résolu
PythonGreg Messages postés 68 Date d'inscription jeudi 3 novembre 2011 Statut Membre Dernière intervention 6 février 2015 - 16 juin 2012 à 08:06
PythonGreg Messages postés 68 Date d'inscription jeudi 3 novembre 2011 Statut Membre Dernière intervention 6 février 2015 - 17 juin 2012 à 01:55
Bonjour est t'il posible de générer plusieur série de chaine aléatoire sans passé par un TTimer
voici la function que j'utilise :

Function KeyRandom : String ;
const
  ChAlpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
begin
  Randomize;
  repeat
   Result := Result + ChAlpha[Random(Length(ChAlpha)) + 1];
  until Length(Result) = 4; // Longueur de la clé
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Memo1.Lines.Add ( KeyRandom + '-' + KeyRandom + '-' + KeyRandom + '-' + KeyRandom );
end;
end.


Merci de votre reponse
http://programmationdelphi.info/

4 réponses

Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
16 juin 2012 à 18:41
Salut,

 ...
implementation
uses Math; // pour RandomRange
{$R *.dfm}

// fonction générant une chaine aléatoire
// de longueur aLen.
function KeyRandom(const aLen: Integer): string;
begin
  Result := EmptyStr;
  if aLen < 1 then
    Exit;
  repeat
    Result := Result + Chr(RandomRange(Ord('A'), Ord('Z') + 1));
  until Length(Result) = aLen;
end;

// Génération et affichage du résultat
procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := KeyRandom(4) + '-' + KeyRandom(4) + '-' + KeyRandom(4) + '-' + KeyRandom(4);
end;

// le Randomize ne s'appelle qu'une seule fois
// c'est pourquoi on le met dans le OnCreate de la fiche
procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
end;

end.



[hr]@+Cirec
[hr]
3
dubois77 Messages postés 675 Date d'inscription jeudi 17 avril 2008 Statut Membre Dernière intervention 19 février 2019 14
16 juin 2012 à 15:19
For i:=1 to xxxx do .....

çà devrait marcher
Dubois77
0
cs_walidlam Messages postés 107 Date d'inscription lundi 30 mai 2011 Statut Membre Dernière intervention 29 avril 2013 1
16 juin 2012 à 16:01
Salut !!
essayer :
Function KeyRandom(Nbr:Integer): String ;
const
  ChAlpha = '013456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
begin
  Randomize;
  repeat
   Result := Result + ChAlpha[Random(Length(ChAlpha)) + 1];
  until Length(Result) = Nbr; // Longueur de la clé
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Add ( KeyRandom(6) + '-' + KeyRandom(4) + '-' + KeyRandom(4) + '-' + KeyRandom(2));
end;

@+
0
PythonGreg Messages postés 68 Date d'inscription jeudi 3 novembre 2011 Statut Membre Dernière intervention 6 février 2015
17 juin 2012 à 01:55
salut,

Merci cirec
de ta reponse sa marche niquel


http://programmationdelphi.info/
0
Rejoignez-nous