Random de label

Résolu
ProGL1v3 Messages postés 13 Date d'inscription samedi 11 avril 2009 Statut Membre Dernière intervention 29 août 2010 - 7 juil. 2010 à 18:30
ProGL1v3 Messages postés 13 Date d'inscription samedi 11 avril 2009 Statut Membre Dernière intervention 29 août 2010 - 13 juil. 2010 à 16:40
slt , comment je fait pour que la commande random ne tire plus les memmes chifres;
exmple: jai 2 label1 et Label2 et je veux fair random sans que les 2 labels prenent la meme valuer,meme si cest le cas lun des deux prend une autre valeur;
label1.caption:=IntToStr(1 + random(5));
label2.caption:=IntTostr(1 + random(5));

ou

J 3 label:(Label1,Label2,label3) et un boutton ;
alors quon je clique sur le botton (random)les Labels prenent une valeur entre 1 et 5 sans quils prenet les meme valeur
exemple :=> si jai 3 label et I:= 1 + random(3);
alores resultat sera comme suit;
LAbel1:=3 LAbel2:=2 LAbel3:=1
LAbel1:=3 LAbel2:=1 LAbel3:=2
LAbel1:=2 LAbel2:=1 LAbel1:=3
merci

8 réponses

dubois77 Messages postés 675 Date d'inscription jeudi 17 avril 2008 Statut Membre Dernière intervention 19 février 2019 14
8 juil. 2010 à 07:57
x2 := 0;
x1 := 1 + random(5);
while x1=x2 do x2 := 1 + random(5);
label1.caption:=IntToStr(x1);
label2.caption:=IntTostr(x2);



Dubois77
3
dubois77 Messages postés 675 Date d'inscription jeudi 17 avril 2008 Statut Membre Dernière intervention 19 février 2019 14
11 juil. 2010 à 10:47
Salut
essaye cela
const nLabels = 6; // valeur à adapter au nombre de labels à remplir
      xRND = 5; // à adapter suivant valeur à obtenir

procedure ChargeLabels;
var TB : array[1..nLabels]; i,j,x:integer;   FLG:boolean;
begin
  for i:=1 to NLabels do TB[i] := -1; // ou autre valeur non tirée par le random 
  x := 1 + random(xRND);
  TB[1] := x;
  Label1.caption := inttostr(x);
  for j:=2 to nLabels do
    begin
    FLG := true;
    while FLG do
      begin
      FLG := false;
      x := 1 + random(xRND);
      for j:=1 to nLabels do
        if TB[j]=x then FLG := true
      end; 
    TB[j] := x;
    Tlabel(FindComponent('Label' + inttostr(j))).caption := inttostr(x);
    end;
end;
:


Dubois77
3
dubois77 Messages postés 675 Date d'inscription jeudi 17 avril 2008 Statut Membre Dernière intervention 19 février 2019 14
11 juil. 2010 à 11:13
petite erreur : for i et et non pas for j !
for i:=1 to nLabels do
if TB[i]=x then FLG := true
end;

il faut conserver TB[j] := x;

don version correcte
const nLabels 6; // valeur à adapter au nombre de labels à remplir
      xRND = 5; // à adapter suivant valeur à obtenir

procedure ChargeLabels;
var TB : array[1..nLabels]; i,j,x:integer;   FLG:boolean;
begin
  for i:=1 to NLabels do TB[i] := -1; // ou autre valeur non tirée par le random 
  x := 1 + random(xRND);
  TB[1] := x;
  Label1.caption := inttostr(x);
  for j:=2 to nLabels do
    begin
    FLG := true;
    while FLG do
      begin
      FLG := false;
      x := 1 + random(xRND);
      for i:=1 to nLabels do
        if TB[i]=x then FLG := true
      end; 
    TB[j] := x;
    Tlabel(FindComponent('Label' + inttostr(j))).caption := inttostr(x);
    end;
end;


Dubois77
3
dubois77 Messages postés 675 Date d'inscription jeudi 17 avril 2008 Statut Membre Dernière intervention 19 février 2019 14
12 juil. 2010 à 07:51
J'ai testé mon code (ce que j'aurais du faire avant de poster) :
il y avait un bug
Voila l'unit testée, correcte, qui tire les 6 bon numéro du loto (peut être !)
unit Unit1;
//===============================================================
interface

uses
  Windows,
  dialogs,
  Classes,
  Controls,
  Forms, StdCtrls ,
  sysutils;   // pour inttostr

const nLabels = 6; // valeur à adapter au nombre de labels à remplir
      xRND = 49; // à adapter suivant valeur à obtenir

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Button1: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    procedure QUITTER;
    procedure ChargeLabels;

  end;

var
  Form1: TForm1;

//===============================================================
implementation
{$R *.dfm}

//--------------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
begin
randomize
end;

//--------------------------------------------------------------
procedure TForm1.ChargeLabels;
var TB : array[1..nLabels] of integer; i,j,x:integer;   FLG:boolean;
begin
  for i:=1 to NLabels do TB[i] := -1; // ou autre valeur non tirée par le random 
  //x := 1 + random(xRND);
  //TB[1] := x;
  //Label1.caption := inttostr(x);
  for j:=1 to nLabels do
    begin
    FLG := false;
    while not FLG do
      begin
      FLG := true;
      x := 1 + random(xRND);
      for i:=1 to nLabels do if TB[i]=x then FLG := false
      end;
    TB[j] := x;
    end;
  for i:=1 to nLabels do Tlabel(FindComponent('Label' + inttostr(i))).caption := inttostr(TB[i]);
end;

//--------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
ChargeLabels
end;

end.



Dubois77
3

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
beckerich Messages postés 302 Date d'inscription jeudi 29 septembre 2005 Statut Membre Dernière intervention 17 septembre 2013 2
8 juil. 2010 à 14:39
bonjour,

et ne pas oublier l'appel à Randomize...
Luc.
0
ProGL1v3 Messages postés 13 Date d'inscription samedi 11 avril 2009 Statut Membre Dernière intervention 29 août 2010
9 juil. 2010 à 01:32
merci bcp
0
ProGL1v3 Messages postés 13 Date d'inscription samedi 11 avril 2009 Statut Membre Dernière intervention 29 août 2010
10 juil. 2010 à 22:31
j'ai essayer avec while sa marche mais avec 3 labels sa marche pas.
0
ProGL1v3 Messages postés 13 Date d'inscription samedi 11 avril 2009 Statut Membre Dernière intervention 29 août 2010
13 juil. 2010 à 16:40
MErci bcp, javoue tu est un PP(Pro Programimng^^).
0
Rejoignez-nous