
Public Const Campi As Integer = 1 ' numero di campi da formare
Public Const Rec As Integer = 10 ' sequenza numerica (1÷100)
Public Riga As Integer, Valore As Integer, x As Integer, y As Integer, w As Integer
Sub Genera()
Application.ScreenUpdating = False
For x = 1 To Campi
Columns(x).ClearContents
Riga = 1
Do While Riga <= Rec
Valore = Int((Rec * Rnd) + 1)
For y = 1 To w
If Cells(y, x) = Valore Then Ok = 1
Next y
If Ok = 0 Then
Cells(Riga, x) = Valore
Riga = Riga + 1
w = Riga
End If
Ok = 0
Loop
Next x
Application.ScreenUpdating = True
Range("A1").Select
End Sub |
valmax = 20000 'valore massimo
valmin = 1 'valore minimo
ncelle = 100 'numero di celle da occupare
For i = 1 To ncelle 'scrive dalla cella 1 alla cella impostata
a = Int(Rnd() * (valmax - valmin + 1)) + valmin
Set fin = Columns("a").Find(what:=a, lookat:=xlWhole)
If Not fin Is Nothing Then
i = i - 1
Else
Cells(i, 1) = a
End If
Next |
Function genera_lista_from_to(min As Long, max As Long) As Variant
Dim lista() As Long, i As Integer, J As Integer, r1 As Long, r2 As Long, tmp As Long
'genera una lista di numeri casuali non ripetuti nel range specificato
'esempio: u = genera_lista_from_to(1, 10): For Each k In u: Print k;: Next
Randomize Timer
ReDim lista(1 To max - 1) As Long
'genera la lista di numeri consecutivi tra min e max
For i = min To max - 1
lista(i - min + 1) = i
Next
'quindi la disordina
For i = 1 To 1000
r1 = Int(Rnd * (max - min) + min)
r2 = Int(Rnd * (max - min) + min)
'swap
tmp = lista(r1)
lista(r1) = lista(r2)
lista(r2) = tmp
Next
genera_lista_from_to = lista()
End Function |
Sub Genera()
Dim Rec As Integer, Riga As Integer, Valore As Integer, x As Integer
Application.ScreenUpdating = False
x = 1
Rec = 3000
Columns(x).ClearContents
Riga = 1
Do While Riga <= Rec
Valore = Int((Rec * Rnd) + 1)
If Application.WorksheetFunction.CountIf(Range(Cells(1, x), Cells(Riga, x)), Valore) = 0 Then
Cells(Riga, x) = Valore
Riga = Riga + 1
End If
Loop
Application.ScreenUpdating = True
Range("A1").Select
End Sub
|
Option Explicit
Option Base 1
Sub ElencoCasuale()
Const Elementi = 20000
Const MaxValore = 40000
Dim Esiste As Boolean
Dim Riga As Long, i As Long, y As Long, Valore As Long, T As Single
Dim Valori(Elementi)
Application.ScreenUpdating = False
Application.EnableEvents = False
T = Timer
Columns(1).ClearContents
Valori(1) = Int((MaxValore * Rnd) + 1)
For Riga = 2 To Elementi
Do
Valore = Int((MaxValore * Rnd) + 1)
Esiste = False
For y = 1 To Riga
If Valori(y) = Valore Then
Esiste = True
Exit For
End If
Next y
Loop While Esiste
Valori(Riga) = Valore
Next Riga
For Riga = 1 To Elementi
Cells(Riga, 1) = Valori(Riga)
Next Riga
MsgBox Timer - T
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
|
'---------------------------------------------------------------------------------------
' Procedure : GeneraDaA
' Author : scossa
' Date : 25/03/2014
' Purpose :
'---------------------------------------------------------------------------------------
'
Public Function GeneraDaA(ByVal nMin As Long, ByVal nMax As Long) As Variant
Dim cCasual As Collection
Dim nCasual As Double
Dim aCasual As Variant
Dim j As Long
Set cCasual = New Collection
On Error Resume Next
Do While cCasual.Count < (nMax - nMin + 1)
nCasual = Application.WorksheetFunction.RandBetween(nMin, nMax)
cCasual.Add nCasual, CStr(nCasual)
Loop
On Error GoTo 0
ReDim aCasual(1 To cCasual.Count)
For j = 1 To cCasual.Count
aCasual(j) = cCasual.Item(j)
Next j
Set cCasual = Nothing
GeneraDaA = aCasual
End Function
Sub prova()
Dim aprova As Variant
aprova = GeneraDaA(1, 20000)
Debug.Print aprova(1)
End Sub
|
'---------------------------------------------------------------------------------------
' Procedure : GeneraDaA
' Author : scossa
' Date : 25/03/2014
' Purpose :
'---------------------------------------------------------------------------------------
'
Public Function GeneraDaA(ByVal nMin As Long, ByVal nMax As Long) As Variant
Dim cCasual As Collection
Dim nCasual As Double
Dim aCasual As Variant
Dim j As Long
Set cCasual = New Collection
On Error Resume Next
Do While cCasual.Count < (nMax - nMin + 1)
nCasual = Application.WorksheetFunction.RandBetween(nMin, nMax)
cCasual.Add nCasual, CStr(nCasual)
Loop
On Error GoTo 0
ReDim aCasual(1 To cCasual.Count, 1 To 1)
For j = 1 To cCasual.Count
aCasual(j, 1) = cCasual.Item(j)
Next j
Set cCasual = Nothing
GeneraDaA = aCasual
End Function
Sub prova()
Dim aprova As Variant
aprova = GeneraDaA(1, 20000)
Range("A1:A20000") = aprova
End Sub
|
Sub ElencoCasuale()
Const Elementi = 20000
Dim i As Long, Valore As Long, T As Single
Dim Valori(Elementi)
Dim vColl As Collection
Application.ScreenUpdating = False
Application.EnableEvents = False
T = Timer
Columns(1).ClearContents
Set vColl = New Collection
For i = 1 To Elementi
vColl.Add i, CStr(i)
Next i
For i = 1 To Elementi
Valore = Int((vColl.Count * Rnd) + 1)
Cells(i, 1) = vColl(Valore)
vColl.Remove Valore
Next i
MsgBox Timer - T
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
|
Sub ElencoCasuale()
Const Elementi = 20000
Dim i As Long, Valore As Long, T As Single
Dim vColl As Collection
Application.ScreenUpdating = False
Application.EnableEvents = False
T = Timer
Columns(1).ClearContents
Set vColl = New Collection
For i = 1 To Elementi
vColl.Add i
Next i
For i = 1 To Elementi
Valore = Int((vColl.Count * Rnd) + 1)
Cells(i, 1) = vColl(Valore)
vColl.Remove Valore
Next i
MsgBox Timer - T
Application.EnableEvents = True
Application.ScreenUpdating = True
Set vColl = Nothing
End Sub |
Sub RandomArr()
Dim i As Integer, j As Integer, temp As Integer, arr() As Integer, first As Integer, last As Integer
T = Timer
Columns(1).ClearContents
first = 1: last = 10000
ReDim arr(last)
For i = first To last
arr(i) = i
Next
For i = last To first Step -1
j = Rnd * (last - first + 1) + first
If j > last Then j = last
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
Next
Range("A1:A" & last) = Application.Transpose(arr)
MsgBox Timer - T
End Sub
|
Sub GeneraElencoCasuale()
Dim i As Integer, j As Integer, temp As Integer, arr() As Integer, first As Integer, last As Integer, num As Integer
num = [B1].End(xlDown).Row - 1
MsgBox ("Il numero di candidati è " & num)
Columns(1).ClearContents
Range("A1") = "Ordine casuale"
first = 1: last = num
ReDim arr(last)
For i = first To last
arr(i) = i
Next
For i = last To first Step -1
j = Rnd * (last - first + 1) + first
If j > last Then j = last
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
Next
Range(Cells(2, 1), Cells(num + 1, 1)) = Application.Transpose(arr)
Range("A1").Select
MsgBox ("Fine.")
End Sub |
Sub RandomArr2()
Dim i As Long, j As Long, temp As Long, arr() As Long, first As Long
Dim last As Long, T As Single
T = Timer
Columns(1).ClearContents
first = 1: last = 60000
ReDim arr(first To last, 1 To 1)
For i = first To last
arr(i, 1) = i
Next
For i = last To first Step -1
j = Rnd * (last - first + 1) + first
If j > last Then j = last
temp = arr(i, 1)
arr(i, 1) = arr(j, 1)
arr(j, 1) = temp
Next
Range("C1:C" & last) = arr
MsgBox Timer - T
End Sub |
...........
Range("C1:C" & (last - first + 1)) = arr
........... |
