Pour sortir 10 nombres aléatoires compris entre 1 et 60, j'utilise la formule:
=ALEA.ENTRE.BORNES(1;60) en sélectionnant 10 cellules ayant la même formule.
cela fonctionne, mais il y a un "hic", j'ai des doublons parmis les nombres ! Comment faire pour qu'ils soient tous différents ?
Merci de votre aide,
Cécilia.
Yvan
Bonsoir Cécilia
Essaie cette macro (Le test des doublons est surement perfectible).
Sub TirageSansDoublon() Const vMin As Integer = 1 Const vMax As Integer = 60 Dim rngTable As Range Dim N As Integer, i As Integer, j As Integer Dim Val As Integer Set rngTable = Sheets(1).Range("Table") N = rngTable.Rows.Count rngTable.ClearContents i = 0 Randomize While (i < N) iter: Val = Int((vMax - vMin + 1) * Rnd + vMin) For j = 1 To i If rngTable(j, 1) = Val Then GoTo iter Next j i = i + 1 rngTable(i, 1) = Val Wend End Sub
Tu dois juste prévoir une plage 10 lignes/1 colonne et la nommer Table.
@+
Yvan
"" a écrit dans le message de news: % | Bonjour, | | Pour sortir des nombres aléatoires compris entre 1 et 60, j'utilise la | formule: | | =ALEA.ENTRE.BORNES(1;60) | | cela fonctionne, mais il y a un "hic", j'ai des doublons parmis les nombres | ! | Comment faire pour qu'ils soient tous différents ? | | Merci de votre aide, | | Cécilia. | |
Bonsoir Cécilia
Essaie cette macro (Le test des doublons est surement perfectible).
Sub TirageSansDoublon()
Const vMin As Integer = 1
Const vMax As Integer = 60
Dim rngTable As Range
Dim N As Integer, i As Integer, j As Integer
Dim Val As Integer
Set rngTable = Sheets(1).Range("Table")
N = rngTable.Rows.Count
rngTable.ClearContents
i = 0
Randomize
While (i < N)
iter:
Val = Int((vMax - vMin + 1) * Rnd + vMin)
For j = 1 To i
If rngTable(j, 1) = Val Then GoTo iter
Next j
i = i + 1
rngTable(i, 1) = Val
Wend
End Sub
Tu dois juste prévoir une plage 10 lignes/1 colonne et la nommer Table.
@+
Yvan
"Peg@se" <pegase@easynet.be> a écrit dans le message de news:
%23v9GSDEmFHA.3288@TK2MSFTNGP09.phx.gbl...
| Bonjour,
|
| Pour sortir des nombres aléatoires compris entre 1 et 60, j'utilise la
| formule:
|
| =ALEA.ENTRE.BORNES(1;60)
|
| cela fonctionne, mais il y a un "hic", j'ai des doublons parmis les nombres
| !
| Comment faire pour qu'ils soient tous différents ?
|
| Merci de votre aide,
|
| Cécilia.
|
|
Essaie cette macro (Le test des doublons est surement perfectible).
Sub TirageSansDoublon() Const vMin As Integer = 1 Const vMax As Integer = 60 Dim rngTable As Range Dim N As Integer, i As Integer, j As Integer Dim Val As Integer Set rngTable = Sheets(1).Range("Table") N = rngTable.Rows.Count rngTable.ClearContents i = 0 Randomize While (i < N) iter: Val = Int((vMax - vMin + 1) * Rnd + vMin) For j = 1 To i If rngTable(j, 1) = Val Then GoTo iter Next j i = i + 1 rngTable(i, 1) = Val Wend End Sub
Tu dois juste prévoir une plage 10 lignes/1 colonne et la nommer Table.
@+
Yvan
"" a écrit dans le message de news: % | Bonjour, | | Pour sortir des nombres aléatoires compris entre 1 et 60, j'utilise la | formule: | | =ALEA.ENTRE.BORNES(1;60) | | cela fonctionne, mais il y a un "hic", j'ai des doublons parmis les nombres | ! | Comment faire pour qu'ils soient tous différents ? | | Merci de votre aide, | | Cécilia. | |
AV
Pour un tirage de 10 (adapter) valeurs uniques entre (adapter) 1("B_Inf") et 60("B_Sup") dans la plage A1:A10 (adapter) :
Sub zz_SansDoublon()'av B_Sup = 60: B_Inf = 1 For i = 1 To 10 Cells(i, 1) = Evaluate("int(rand()*(" & B_Sup & "-" & B_Inf & ")+" & B_Inf & ")") If i > 1 Then While Not Range(Cells(1, 1), Cells(i - 1, 1)).Find(Cells(i, 1)) Is Nothing Cells(i, 1) = Evaluate("int(rand()*(" & B_Sup & "-" & B_Inf & ")+" & B_Inf & ")") Wend End If Next End Sub
Pour un tirage de 10 (adapter) valeurs uniques entre (adapter) 1("B_Inf") et
60("B_Sup") dans la plage A1:A10 (adapter) :
Sub zz_SansDoublon()'av
B_Sup = 60: B_Inf = 1
For i = 1 To 10
Cells(i, 1) = Evaluate("int(rand()*(" & B_Sup & "-" & B_Inf & ")+" & B_Inf &
")")
If i > 1 Then
While Not Range(Cells(1, 1), Cells(i - 1, 1)).Find(Cells(i, 1)) Is
Nothing
Cells(i, 1) = Evaluate("int(rand()*(" & B_Sup & "-" & B_Inf & ")+" &
B_Inf & ")")
Wend
End If
Next
End Sub
Pour un tirage de 10 (adapter) valeurs uniques entre (adapter) 1("B_Inf") et 60("B_Sup") dans la plage A1:A10 (adapter) :
Sub zz_SansDoublon()'av B_Sup = 60: B_Inf = 1 For i = 1 To 10 Cells(i, 1) = Evaluate("int(rand()*(" & B_Sup & "-" & B_Inf & ")+" & B_Inf & ")") If i > 1 Then While Not Range(Cells(1, 1), Cells(i - 1, 1)).Find(Cells(i, 1)) Is Nothing Cells(i, 1) = Evaluate("int(rand()*(" & B_Sup & "-" & B_Inf & ")+" & B_Inf & ")") Wend End If Next End Sub