Imports System.IO
Imports System.Drawing.Image
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Button2.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("F:\Wallpaper"))
Button3.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("F:\Wallpaper"))
Button4.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("F:\Wallpaper"))
Button7.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("F:\Wallpaper"))
Button6.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("F:\Wallpaper"))
Button5.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("F:\Wallpaper"))
Button10.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("F:\Wallpaper"))
Button9.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("F:\Wallpaper"))
Button8.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("F:\Wallpaper"))
End Sub
Public Function GetRandomImageFilePath(ByVal folderPath As String) As String
Dim files() As String = Directory.GetFiles(folderPath, "*.jpg")
Dim random As Random = New Random()
Return files(random.Next(0, files.Length - 1))
End Function
End Class
Imports System.IO
Imports System.Drawing.Image
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button2.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("images"))
Button3.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("images"))
Button4.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("images"))
Button7.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("images"))
Button6.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("images"))
Button5.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("images"))
Button10.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("images"))
Button9.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("images"))
Button8.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("images"))
Button11.Image = System.Drawing.Image.FromFile(GetRandomImageFilePath("images"))
End Sub
Public Function GetRandomImageFilePath(ByVal folderPath As String) As String
Dim files() As String = Directory.GetFiles(folderPath, "*.png")
Dim random As Random = New Random()
Return files(random.Next(10, files.Length - 50))
End Function
End Class
ใช้ตัวนี้แทนครับ จะได้ชุดตัวเลขออกมาไม่ซ้ำตามจำนวนที่ระบุใน AllNum เรียกว่าใช้หลักการ ของ HashSet เข้ามาช่วยครับ เพราะยังไง Random มันต้องซ้ำอยู่แล้วครับ
Code (VB.NET)
Public Class Form1
Sub RandomNumNotDuplicate()
Dim I As Integer
Dim NumRan As HashSet(Of Integer) = New HashSet(Of Integer)()
Dim AllNum As Integer
AllNum = 10
For I = 1 To AllNum
Randomize()
NumRan.Add(CInt(Math.Ceiling(Rnd() * AllNum)))
If (I = (AllNum - 1)) Then
If (NumRan.Count <> 10) Then
I = 1
End If
End If
Next
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
RandomNumNotDuplicate()
End Sub
End Class