VB洗牌算法产生随机数组
Posted 一个码农的蹉跎岁月
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于VB洗牌算法产生随机数组的主要内容,如果未能解决你的问题,请参考以下文章