作者patw (小p)
看板Visual_Basic
标题Re: [.NET] 如何让字机率性的出现?
时间Sat Nov 21 20:35:23 2009
最近刚开始学习 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