Bonsoir et merci,Bonjour
En A1
=ALEA()
En B1
=RANG(A1;$A$1:$A$50)
Puis recopier vers le bas jusqu'à la ligne 50
PS: Il y aura parfois des doublons mais à la marge.
Sub Pour_Test()
[A1:A50].FormulaR1C1 = "=RANDBETWEEN(1,50)"
[B1:B50].FormulaR1C1 = "=RANK.EQ(RC[-1],R1C1:R50C1)+COUNTIF(R1C1:RC[-1],RC[-1])-1"
End Sub
Sub Pour_Test_O365()
[A1].Formula2R1C1 = "=RANDARRAY(50,1,1,50,1)"
[B1:B50].FormulaR1C1 = "=RANK.EQ(RC[-1],R1C1:R50C1)+COUNTIF(R1C1:RC[-1],RC[-1])-1"
End Sub
Sub Tirages()
Dim N, i, r
N = Val(Application.InputBox("Nombre entier entre 1 et 50 :"))
If N <> Int(N) Or N < 1 Or N > 50 Then Exit Sub
ReDim a(1 To N, 1 To 1)
For i = 1 To N
Do
r = Application.RandBetween(1, 50)
Loop While IsNumeric(Application.Match(r, a, 0))
a(i, 1) = r
Next
'---restitution---
Application.ScreenUpdating = False
With [A2]
.Resize(N) = a
.Resize(N).Sort .Cells, xlAscending, Header:=xlNo 'tri
.Offset(N).Resize(Rows.Count - N - .Row + 1).ClearContents 'RAZ en dessous
End With
End Sub
=SIERREUR(TRANSPOSE(INDEX(TRIER(ASSEMB.V(SEQUENCE(1;A2);TABLEAU.ALEA(1;A2));2;1;1);1;0));"")
Private Sub Worksheet_Change(ByVal Target As Range)
Dim N&
If Intersect(Target, [a2]) Is Nothing Then Exit Sub
N = [a2]
[b2].Resize(Rows.Count - 1).ClearContents
If N < 1 Then Exit Sub
[b2].Formula2 = Replace("=TRANSPOSE(INDEX(SORT(VSTACK(SEQUENCE(1,xx),RANDARRAY(1,xx)),2,1,1),1,0))", "xx", N)
[b2].Resize(N) = [b2].Resize(N).Value
End Sub
Function Alea_N_Min_Max(ByVal combien&, ByVal Min&, ByVal Max&)
Dim i&, j&, k&, n&, aux
If Min > Max Or combien <= 0 Or combien > Abs(Min - Max) + 1 Then ReDim r(1 To 1, 1 To 1): r(1, 1) = CVErr(xlErrRef): Alea_N_Min_Max = r: Exit Function
n = (Max - Min + 1): Randomize
If combien = 1 Then ReDim t(1 To 1, 1 To 1): t(1, 1) = "": Alea_N_Min_Max = Min + Int(n * Rnd): Exit Function
ReDim t(Min To Max, 1 To 1): For i = Min To Max: t(i, 1) = i: Next
For j = 1 To 5: For i = Min To Max: k = Min + Int(n * Rnd): aux = t(i, 1): t(i, 1) = t(k, 1): t(k, 1) = aux: Next i, j
ReDim r(1 To combien, 1 To 1): For i = 1 To combien: r(i, 1) = t(Min + i - 1, 1): Next
Alea_N_Min_Max = r
End Function
Sub Hop()
Dim x
Application.ScreenUpdating = False
Range("c2").Resize(Rows.Count - 1).ClearContents
x = Alea_N_Min_Max([a6], [a2], [a4])
Range("c2").Resize(UBound(x)) = x
' tri optionnel
'Range("c2").Resize(UBound(x)).Sort key1:=Range("c2"), order2:=1, Header:=xlNo
End Sub
bonjour à tousBonsoir et bon dimanche,
en vba , comment générer aléatoirement dans une colonne du plus petit au plus grand une série de nombres entiers, sans doublon, compris entre 1 et 50?
Merci d'avance,
Claudy
Sub test()
Dim tbl, maxQ, Minx
Minx = 1: maxQ = 50
tbl = GenerateWithoutDouble(Minx, maxQ)
[A1].Resize(maxQ) = tbl
End Sub
'la fonction
Function GenerateWithoutDouble(Minx, maxQ)
Dim x, i&, y&
x = Evaluate("ROW(" & Minx & ":" & maxQ & ")")'création du tableau de minx à maxq
For i = 1 To UBound(x)'mélange du premier au dernier
y = (1 + (Rnd * 49)): temp = x(i, 1)
x(i, 1) = x(y, 1): x(y, 1) = temp
GenerateWithoutDouble = x'return du tableau
Next
End Function