hela_bouks
Messages postés18Date d'inscriptionvendredi 24 février 2006StatutMembreDernière intervention 2 janvier 2008
-
23 juil. 2007 à 10:14
cs_EBArtSoft
Messages postés4525Date d'inscriptiondimanche 29 septembre 2002StatutModérateurDernière intervention22 avril 2019
-
23 juil. 2007 à 15:05
Salut,
J'ai un tableau de vingt éléments P1, P2, ..., P20 et je voudrai, à partir de ce tableau, en créer un autre contenant les mêmes éléments mais dans un ordre complétement aléatoire par exemple P4,P8,P15,P1,P19,...
Merci beaucoup de votre aide
jrivet
Messages postés7392Date d'inscriptionmercredi 23 avril 2003StatutMembreDernière intervention 6 avril 201260 23 juil. 2007 à 10:26
Salut,
Regarde ce petit code.
Je suis sûr qu'il y a plus optimal, mais l'idée est là.
Option Explicit
'Tableau source
Private Source(1 To 20) As Integer
'Tableau Destination
Private Destination(1 To 20) As Integer
'Tableau mémoire des indices déjà récupérer
Private IndicesFaits(1 To 20) As Boolean
Private Sub Form_Load()
Dim i As Integer
Dim IndiceCourant As Integer
'on remplit la source de 1 à 20
For i = 1 To 20
Source(i) = i
Next
Randomize
'indice courant du tableau de destination
IndiceCourant = 1
'tant que tout les indices
'n'aurotn pas été tirés
While Not ToutFait
'i tiré au hasard
i = Int((20 * Rnd) + 1)
'si i n'est pas un indice déjà fait
If Not IndicesFaits(i) Then
Destination(IndiceCourant) = Source(i)
'memorise que l'indice est passé
IndicesFaits(i) = True
'incrémentation de indicecourant
IndiceCourant = IndiceCourant + 1
End If
Wend
End Sub<hr />
Private Function ToutFait() As Boolean
Dim i As Integer
ToutFait = True
For i = 1 To 20
If Not IndicesFaits(i) Then
ToutFait = False
Exit For
End If
Next
End Function , ----
[code.aspx?ID=41455 By Renfield]
cs_EBArtSoft
Messages postés4525Date d'inscriptiondimanche 29 septembre 2002StatutModérateurDernière intervention22 avril 20199 23 juil. 2007 à 12:45
Sub ShuffleTable(ByRef t() As Long)
Dim tmp As New Collection
Dim i As Long
Dim n As Long
Randomize Timer
For i = 0 To UBound(t)
tmp.Add i
Next
For i = 0 To UBound(t)
n = 1 + Int(Rnd * tmp.Count)
t(i) = tmp(n)
tmp.Remove n
Next
End Sub
cs_EBArtSoft
Messages postés4525Date d'inscriptiondimanche 29 septembre 2002StatutModérateurDernière intervention22 avril 20199 23 juil. 2007 à 15:05
Ou bien ceci :
Sub ShuffleTable2(ByRef t() As Long)
Dim p() As Long
Dim i As Long
Dim k As Long
Randomize Timer
k = UBound(t)
ReDim p(k)
For i = 0 To k
p(i) = t(i)
Next
While (k >= 0)
i = Int(Rnd * k)
t(k) = p(i)
p(i) = p(k)
k = k - 1
Wend
End Sub