非重复随机数发生器?

时间:2011-09-24 23:28:29

标签: vba random generator

我使用Visual Basic for applications(Excel)创建了一个琐事游戏,通过查看案例为数字的案例陈述来选择问题。我让程序随机选择一个从1到最大问题数量的数字。使用这种方法,游戏会重复问题。

有没有办法制作随机生成数字的东西(每次都有不同的结果)并且不会重复多次?在经历了所有数字之后,它需要执行某个代码。 (我会输入结束游戏的代码,并显示他们得到的问题和错误的数量)

我想到了几种不同的方法,但是我甚至无法想到语法可能是什么。

4 个答案:

答案 0 :(得分:7)

听起来你需要一个Array Shuffler!

查看以下链接 - http://www.cpearson.com/excel/ShuffleArray.aspx

Function ShuffleArray(InArray() As Variant) As Variant()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ShuffleArray
' This function returns the values of InArray in random order. The original
' InArray is not modified.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim N As Long
    Dim Temp As Variant
    Dim J As Long
    Dim Arr() As Variant


    Randomize
    L = UBound(InArray) - LBound(InArray) + 1
    ReDim Arr(LBound(InArray) To UBound(InArray))
    For N = LBound(InArray) To UBound(InArray)
        Arr(N) = InArray(N)
    Next N
    For N = LBound(InArray) To UBound(InArray)
        J = CLng(((UBound(InArray) - N) * Rnd) + N)
        Temp = InArray(N)
        InArray(N) = InArray(J)
        InArray(J) = Temp
    Next N
    ShuffleArray = Arr
End Function

Sub ShuffleArrayInPlace(InArray() As Variant)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ShuffleArrayInPlace
' This shuffles InArray to random order, randomized in place.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim N As Long
    Dim Temp As Variant
    Dim J As Long

    Randomize
    For N = LBound(InArray) To UBound(InArray)
        J = CLng(((UBound(InArray) - N) * Rnd) + N)
        If N <> J Then
            Temp = InArray(N)
            InArray(N) = InArray(J)
            InArray(J) = Temp
        End If
    Next N
End Sub

答案 1 :(得分:5)

这是另一个需要。它会生成一系列独特的随机长片。 在这个例子中,我使用1到100.它通过使用集合对象来实现。然后你就可以在qArray中对每个数组元素进行正常循环,而不需要多次随机化。

Sub test()
Dim qArray() As Long
ReDim qArray(1 To 100)

qArray() = RandomQuestionArray
'loop through your questions

End Sub

Function RandomQuestionArray()  
Dim i As Long, n As Long
Dim numArray(1 To 100) As Long
Dim numCollection As New Collection

With numCollection
    For i = 1 To 100
        .Add i
    Next
    For i = 1 To 100
        n = Rnd * (.Count - 1) + 1
        numArray(i) = numCollection(n)
        .Remove n
    Next
End With

RandomQuestionArray = numArray()

End Function

答案 2 :(得分:2)

我看到你有答案,我正在努力解决这个问题,但却丢失了我的网络连接。无论如何,这是另一种方法。

'// Builds a question bank (make it a hidden sheet)
Sub ResetQuestions()
    Const lTotalQuestions As Long = 300 '// Total number of questions.

    With Range("A1")
        .Value = 1
        .AutoFill Destination:=Range("A1").Resize(lTotalQuestions), Type:=xlFillSeries
    End With

End Sub
'// Gets a random question number and removes it from the bank
Function GetQuestionNumber()
    Dim lCount As Long   

    lCount = Cells(Rows.Count, 1).End(xlUp).Row      

    GetQuestionNumber = Cells(Int(lCount * Rnd + 1), 1).Value

    Cells(lRandom, 1).Delete
End Function

Sub Test()

    Msgbox (GetQuestionNumber)

End Sub

答案 3 :(得分:0)

无论这里有什么价值,我都会抓住这个问题。这个使用布尔函数而不是数值数组。它非常简单但非常快。它的优点,我不是说是完美的,是长距离数字的有效解决方案,因为你只检查你已经选择并保存的数字,并且不需要一个可能很大的数组来保存数值你已经拒绝,所以它不会因为数组的大小而导致内存问题。

Sub UniqueRandomGenerator()
Dim N As Long, MaxNum As Long, MinNum As Long, Rand As Long, i As Long

MinNum = 1        'Put the input of minimum number here
MaxNum = 100      'Put the input of maximum number here
N = MaxNum - MinNum + 1

ReDim Unique(1 To N, 1 To 1)

For i = 1 To N
Randomize         'I put this inside the loop to make sure of generating "good" random numbers
    Do
        Rand = Int(MinNum + N * Rnd)
        If IsUnique(Rand, Unique) Then Unique(i, 1) = Rand:  Exit Do
    Loop
Next
Sheet1.[A1].Resize(N) = Unique
End Sub

Function IsUnique(Num As Long, Data As Variant) As Boolean
Dim iFind As Long

On Error GoTo Unique
iFind = Application.WorksheetFunction.Match(Num, Data, 0)

If iFind > 0 Then IsUnique = False: Exit Function

Unique:
    IsUnique = True
End Function