Comment detail

n人中m人が当選するくじ (Nested Flatten)
mがnに近くなると効率悪くなりますが。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Public Sub DrawLots(ByVal n As Integer, ByVal m As Integer)
    If n < m OrElse n < 1 Then
        Exit Sub
    End If

    Dim list As New List(Of Integer)
    Dim r As New Random

    Do
        Dim v As Integer = r.Next(1, n + 1)
        If Not list.Contains(v) Then
            list.Add(v)
            If list.Count = m Then
                Exit Do
            End If
        End If
    Loop

    list.Sort()

    For Each i As Integer In list
        Console.WriteLine(i)
    Next
End Sub

Index

Feed

Other

Link

Pathtraq

loading...