看板 Visual_Basic 關於我們 聯絡資訊
最近剛開始學習 ASP.NET(C#),試了一下。 寫完才發現這邊是 VB 版阿 :P 所以換成 VB 再寫一次, 希望沒寫錯,獻醜囉 ^_^" 第一種是參考上面推文的板友們的取字串式解法: '產生特定數量的字串 Dim str As String = New String("A", 33) + New String("B", 37) + New String("C", 30) Dim rnd As Random = New Random() Response.Write(str.Substring(rnd.Next(0, 99), 1)) 第二種是參考廣告權重露出的寫法 :P Protected Function GetRnd(ByVal rnd As Random) As String Dim ad(3) As String ad(0) = "A" ad(1) = "B" ad(2) = "C" '權重 Dim impressions(3) As Integer impressions(0) = 33 impressions(1) = 37 impressions(2) = 30 Dim total As Integer = 0 For i As Integer = 0 To impressions.Length - 1 total += impressions(i) Next Dim rnd_num As Integer = rnd.Next(1, total) Dim final As String = "" total = 0 For i As Integer = 0 To impressions.Length - 1 total += impressions(i) If rnd_num <= total Then final = ad(i) Exit For End If Next Return final End Function 然後在 Page_Load 裡面叫它 '亂數 Dim r As Random = New Random() '跑個一萬遍看看 :P For i As Integer = 1 To 10000 Dim x As String = GetRnd(r) Response.Write(x) Next -- ═════════════════════════════════════ ███◤◢█◣◢█◣ █◤ ◢◤ ◣ ◢ by hitecc ██◤◢◤◢◤◢◤◢ ██◤ ◢◤◢◣◢◤   █◤◢◤◥◤ ◢◤◢◣◥█◤◥◤ telnet://patw.twbbs.org ═════════════════════════════════ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.25.56.139