Création d'un tableau de chiffres aléatoires sans doublon
Salut
Tout est dans le titre
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| function LstSansDbl(min, max) 'procedure creation d'un tableau de chiffre aléatoire, sans doublon
Dim i
Dim NumAlea 'Indice aléatoire
Dim TblNb() 'Tableau/Arrays pour contenir les chiffres de mini à maxi
Dim TblAlea() 'Tableau/Arrays pour contenir les chiffres aléatoire de la plage min à max
Redim TblAlea(max - min)
Redim TblNb(max - min)
For i = 0 To (max-min): TblNb(i) = min+i: next ' création du tableau/Arrays de min à max
' création du tableau/Arrays aléatoire, plage min à max
Randomize()
For i = (max-min) To 0 step -1
NumAlea = int((i + 1) * Rnd())' tirage pseudo aléatoire
TblAlea(max - min - i) = TblNb(NumAlea)
If NumAlea < i Then TblNb(NumAlea) = TblNb(i)
next
LstSansDbl = TblAlea
End Function
'----------------------------------------------------------------------------------------------------
Dim Mini, Maxi, Retour,T, MsG, Response
Mini = 1: Maxi = 10
Do Until Response = vbNo
MsG = ""
RetouR = LstSansDbl(Mini, Maxi) ' recuperation d'une liste de chiffres aléatoire dans la plage mini à maxi
'Pour la démo, affichage
For T = 0 to Maxi-Mini
MsG = MsG & "Tirage indice " & T & " = " & RetouR(T)
If T <> Maxi-Mini Then Msg = MsG & Vbcrlf
Next
MsgBox MsG
Response = MsgBox ("Recommencer? ", vbYesNo)
Loop |
Ce tableau peut service d'indice pour un autre tableau de façon à faire un tirage aléatoire sans doublon.
Exemple:
vous avez 10 sortes de fruits, vous devez faire les mélanges et prendre les 3 premiers pour faire des cocktails, voici la solution
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| function LstSansDbl(min, max) 'procedure creation d'un tableau de chiffre aléatoire, sans doublon
Dim i
Dim NumAlea 'Indice aléatoire
Dim TblNb() 'Tableau/Arrays pour contenir les chiffres de mini à maxi
Dim TblAlea() 'Tableau/Arrays pour contenir les chiffres aléatoire de la plage min à max
Redim TblAlea(max - min)
Redim TblNb(max - min)
For i = 0 To (max-min): TblNb(i) = min+i: next ' création du tableau/Arrays de min à max
' création du tableau/Arrays aléatoire, plage min à max
Randomize()
For i = (max-min) To 0 step -1
NumAlea = int((i + 1) * Rnd())' tirage pseudo aléatoire
TblAlea(max - min - i) = TblNb(NumAlea)
If NumAlea < i Then TblNb(NumAlea) = TblNb(i)
next
LstSansDbl = TblAlea
End Function
'--------------------------------------------------------------------------
Dim TblFruits(9)
TblFruits(0)="Pomme"
TblFruits(1)="Poire"
TblFruits(2)="Banane"
TblFruits(3)="Orange"
TblFruits(4)="Ananas"
TblFruits(5)="Citron"
TblFruits(6)="Figue"
TblFruits(7)="Cerise"
TblFruits(8)="Mangue"
TblFruits(9)="Mirabelle"
Dim Mini, Maxi, Retour,T, MsG, Response
Mini = LBound(TblFruits): Maxi = UBound(TblFruits)
Do Until Response = vbNo
MsG = ""
RetouR = LstSansDbl(Mini, Maxi) ' recuperation d'une liste de chiffres aléatoire dans la plage mini à maxi
'Pour la démo, affichage
For T = 0 to 2 'Maxi-Mini
MsG = MsG & "Tirage indice " & T & " = " & TblFruits(RetouR(T))
If T <> Maxi-Mini Then Msg = MsG & Vbcrlf
Next
MsgBox MsG
Response = MsgBox ("Recommencer? ", vbYesNo)
Loop |
A votre santé :lol: