Private Declare Function CoCreateGuid Lib "OLE32.DLL" (pGUID As GUID) As Long
Private Declare Function StringFromGUID2 Lib "OLE32.DLL" (pGUID As GUID, ByVal PointerToString As Long, ByVal MaxLength As Long) As Long
Private Type GUID '定义UID
Guid1 As Long
Guid2 As Long
Guid3 As Long
Guid4(0 To 7) As Byte
End Type
'***************************************
'自动产生一个36位的GUID
'***************************************
Public Function CreateGUID() As String
Dim udtGUID As GUID
Dim sGUID As String
Dim lResult As Long
lResult = CoCreateGuid(udtGUID)
If lResult Then
sGUID = ""
Else
sGUID = String$(38, 0)
StringFromGUID2 udtGUID, StrPtr(sGUID), 39
End If
CreateGUID = sGUID
End Function
全球唯一码生成,绝对不会重复
Dim d(19) As Integer
Randomize Timer
For a = 0 To 19
b = Int(100 * Rnd)
For c = 0 To 19
If b = d(c) Then
a = a - 1
Exit For
End If
Next c
d(a) = b
Next a
Private Sub Command1_Click()
Dim a(20) As Long, i As Long, j As Long
For i = 1 To 20
Randomize Timer
l:
a(i) = Rnd * 50 + 50
For j = 1 To i - 1
If a(j) = a(i) Then GoTo l
Next j
Debug.Print a(i)
Next i
End Sub
Option Explicit
'注意引用microsoft scripting runtime
Private Sub Command1_Click()
Dim numarr(1 To 20) As Long
Dim mydict As New Dictionary
Dim i As Long, num As Long
i = 1
Do
Randomize
num = CLng(Rnd * 65535)'这里的65535你可以取小些,以适应你的要求
If mydict.Exists(num) = False Then
mydict.Add num, CStr(i)
numarr(i) = num
i = i + 1
End If
Loop While i <= 20
End Sub
dim blnF as boolean
dim lngAry() as long ,lngI as long ,lngK as long ,lngU as long ,lngII as long
lngU = 19' base 0
redim lngAry(lngU)
randomize timer
for lngI = 0 to lngU
lngK = int(rnd() * 20 + 1)
blnF = false
do
for lngII = 0 to lngI
if lngAry(lngII) = lngK then
blnF = true
exit for
end if
next lngII
loop while (blnF) '这个地方测试一下
lngAry(lngI) = lngK
next lngI
Private Sub Command1_Click()
Dim a(20) As Long, i As Long, j As Long
For i = 1 To 20
Randomize Timer
l:
a(i) = Rnd * 50 + 50
For j = 1 To i - 1
If a(j) = a(i) Then GoTo l
Next j
Debug.Print a(i)
Next i
End Sub
Private Sub Command1_Click()
Dim a(20) As Long, i As Long, j As Long
For i = 1 To 20
Randomize Timer
l:
a(i) = Rnd * 50 + 50
For j = 1 To i - 1
If a(j) = a(i) Then GoTo l
Next j
Debug.Print a(i)
Next i
End Sub
msgbox Int((20 * Rnd) + 1) ' Generate random value between 1 and 20.
Rnd Function
Returns a Single containing a random number.
Syntax
Rnd[(number)]
The optional number argument is a Single or any valid numeric expression.
Return Values
If number is Rnd generates
Less than zero The same number every time, using number as the seed.
Greater than zero The next random number in the sequence.
Equal to zero The most recently generated number.
Not supplied The next random number in the sequence.
Remarks
The Rnd function returns a value less than 1 but greater than or equal to zero.
The value of number determines how Rnd generates a random number:
For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.
Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.
To produce random integers in a given range, use this formula:
Here, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.
Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.