Comment expliques-tu alors que avec ce code:
For ctr As Integer = 0 To 4
ctr -= 1
TextBox1.Text &= i & " "
D'autre part, ton code pose un problème, car si tu tires dans ce sens par exemple :
32, 56, 42, 95, 3, 12
Le 3 ne sera pas pris en compte car il est déjà présent alors que ce n'est pas logique.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questiondim v (93) as integer dim plein as boolean dim g as integer for i = 1 to 93 v(i) = 0 ' charge à RAZ next i genere: g = GENERATION DU NOMBRE ENTIER 1 A 93 ICI ' plein = true for i = 1 to 93 if v(i) = 0 then plein = false exit for end if next i if plein = true then goto fin ' tableau terminé for i = 1 to 93 if v(i) = g then goto genere next i for i = 1 to 93 ' attribue if v(i) = 0 then v(i) = g goto genere end if next i fin:
Public Function GetAleatoire() as Integer() Dim rand As New Random() Dim i As Integer Dim lLstNb as List(Of Integer)=new List(Of Integer) dim lResult as List(Of Integer)=new List(Of Integer) For i=1 to 93 lLstNb.Add(i) next i Do while lLstNb.Count>1 i=rand.Next(lLstNb.Count-1) lResult.Add(lLstNb(i) lLstNb.RemoveAt(i) Loop lResult.Add(lLntNb(0) Return lResult.ToArray() End Function
Public Function GetAleat() as Integer() dim ltResult as List(Of Integer)=new List(Of Integer) Dim rand As New Random() Do While ltResult.Count<93 Dim lNb As integer=rand.Next(92)+1 if Not ltResult.Contains(lNb) then ltResult.Add(lNb) end if loop Return ltResult.ToArray End Function
Dim Tirage As String Dim rand As New Random() Dim i As Integer Dim Numeros As String() Dim Doublon As Boolean For NombreDeTirages As Integer = 1 To 10 Tirage = "" For ctr As Integer = 0 To 4 Doublon = False i = rand.Next(93) + 1 Numeros = Tirage.Split(New [Char]() {" "}) For Each Chiffre In Numeros If Chiffre = i.ToString Then ctr -= 1 : Doublon = True Next If Doublon = False Then Tirage &= i.ToString & " " Next Doublon = False For Each Ligne In ListBox1.Items If Ligne = Tirage Then NombreDeTirages -= 1 : Doublon = True Next If Doublon = False Then ListBox1.Items.Add(Tirage) Next
'Un code inexpressif... en C
ctr -= 1
TextBox1.Text &= i & " "
ctr = ctr - 1 TextBox1.Text = TextBox1.Text + " "
Mais c'est 93 numéros qui doivent être produits dans un ordre peseudo aléatoire, et non 5.
For NombreDeTirages As Integer = 1 To 10 'Nombre de tirages For ctr As Integer = 0 To 4 'Nombre de chiffres par tirage
For NombreDeTirages As Integer = 1 To 1000 'Nombre de tirages For ctr As Integer = 0 To 93 'Nombre de chiffres par tirage
Ton code boucle comme un forcené, car les petits numéros sont peu tirés, ce qui plante le prpogramme !
il respect les règle de Basic (Qbasic), il est découpé comprégensible, gènère tout d'une coup est est ummédiatement utilisable, question de goût...
' 0.82s pour 1000000 d'éléments en moyenne sur 100 itérations Public Function Shuffle(ByVal min As Integer, ByVal max As Integer) As Integer() Static rnd As New Random ' on multiplie la plage de rnd par 100 pour diminuer les collisions Dim count = max - min + 1 Return Enumerable. Range(min, count). OrderBy(Function(n) rnd.Next(100 * count)). ToArray End Function
' 0.13s pour 1000000 d'éléments en moyenne sur 100 itérations Public Function Shuffle2(ByVal min As Integer, ByVal max As Integer) As Integer() Static rnd As New Random Dim count = max - min + 1 Dim range = Enumerable.Range(min, count).ToArray For i = 0 To count - 1 Dim j = rnd.Next(count) Dim tmp = range(i) range(i) = range(j) range(j) = tmp Next Return range End Function
Randmize timer avant l'instruction Rnd