VB洗牌算法产生随机数组
算法图示:
运行效果:
详细代码:
Option Explicit '洗16张牌(0-15),方便用十六进制显示 Private Const CARDMAX = 15 Dim Card() As Long Private Sub 洗牌() Dim i&, l&, r&, t& l = CARDMAX For i = 0 To CARDMAX r = Rnd * l t = Card(l) Card(l) = Card(r) Card(r) = t l = l - 1 Next i End Sub Private Sub Command1_Click() Call 洗牌 '输出洗牌结果和牌点总和 Dim i As Long Dim n As Long For i = 0 To CARDMAX Print Hex(Card(i)); n = n + Card(i) Next i Print " - SUM:" & n End Sub Private Sub Form_Load() Randomize '初始化牌 ReDim Card(CARDMAX) Dim i As Long For i = 0 To CARDMAX Card(i) = i Next i End Sub
分享是追求进步的态度