CInt(Math.Ceiling(Rnd() * n))
โดยที่
n คือ ค่าสูงสุดที่จะทำการสุ่ม
ตัวอย่างการใช้
Code (VB.NET)
Sub Main()
Dim Result as Integer
Result = CInt(Math.Ceiling(Rnd() * 10))
Console.WriteLine("Random Number Is " & Result)
Console.ReadKey()
End Sub
Code (VB.NET)
Sub Main()
Dim Result As Integer
Dim I As Integer
For I = 1 To 10
Result = CInt(Math.Ceiling(Rnd() * 10))
Console.WriteLine("Random Number Is " & Result)
Next
Console.ReadKey()
End Sub
Imports System.Data
Imports System.Data.OleDb
Public Class Form2
Public myconn As OleDbConnection
Public strconn As String
Public ds As DataSet
Public da As OleDbDataAdapter
Public i As Int16
Public topic As Int16 = 0
Public count As Integer
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'GameDataSet.Question' table. You can move, or remove it, as needed.
Me.QuestionTableAdapter.Fill(Me.GameDataSet.Question)
i = 0
topic = 0
condb()
showdata()
count = 0
End Sub
Public Sub condb()
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=D:\ไอน์สไตน์ My Style\ไอน์สไตน์ My Style\Game.mdb"
Try
myconn = New OleDbConnection(strconn)
myconn.Open()
ds = New DataSet
da = New OleDbDataAdapter("select * from Question", myconn)
da.Fill(ds, "Question")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Sub showdata()
Label1.Text = "คำถาม ข้อที่ : " & i + 1 & " ถามว่า " & vbCrLf & ds.Tables("Question").Rows(i)("Question")
Button1.Text = "ข้อ 1 :" & ds.Tables(0).Rows(i)("ans1")
Button2.Text = "ข้อ 2 :" & ds.Tables(0).Rows(i)("ans2")
Button3.Text = "ข้อ 3 :" & ds.Tables(0).Rows(i)("ans3")
Button4.Text = "ข้อ 4 :" & ds.Tables(0).Rows(i)("ans4")
End Sub
Public Sub uplevel()
If topic <= 20 Then
topic += 1
win()
Else
End If
End Sub
Public Sub win()
If topic = 20 Then
MsgBox("ยินดีด้วยคุณเก่งมาก")
Me.Close()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ds.Tables(0).Rows(i)("ans") = 1 Then
MsgBox("เป็นคำตอบที่ถูกต้อง")
If i >= ds.Tables(0).Rows.Count - 1 Then
Else
i += 1
showdata()
uplevel()
End If
Else
MsgBox("เป็นคำตอบที่ผิด")
count += 1
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If ds.Tables(0).Rows(i)("ans") = 2 Then
MsgBox("เป็นคำตอบที่ถูกต้อง")
If i >= ds.Tables(0).Rows.Count - 1 Then
Else
i += 1
showdata()
uplevel()
End If
Else
MsgBox("เป็นคำตอบที่ผิด")
count += 1
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If ds.Tables(0).Rows(i)("ans") = 3 Then
MsgBox("เป็นคำตอบที่ถูกต้อง")
If i >= ds.Tables(0).Rows.Count - 1 Then
Else
i += 1
showdata()
uplevel()
End If
Else
MsgBox("เป็นคำตอบที่ผิด")
count += 1
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If ds.Tables(0).Rows(i)("ans") = 4 Then
MsgBox("เป็นคำตอบที่ถูกต้อง")
If i >= ds.Tables(0).Rows.Count - 1 Then
Else
i += 1
showdata()
uplevel()
End If
Else
MsgBox("เป็นคำตอบที่ผิด")
count += 1
End If
End Sub
End Class