码迷,mamicode.com
首页 > 编程语言 > 详细

VB洗牌算法产生随机数组

时间:2016-04-07 07:06:23      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

算法图示:

技术分享

运行效果:

技术分享

详细代码:

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洗牌算法产生随机数组

标签:

原文地址:http://www.cnblogs.com/youyouran/p/5361956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!