使用RND和Randomize 函数
Randomize 初始化随机数种子
然后用下面的方式生成一个在Max和Min之间的随机整数
Int((Max- Min+ 1) * Rnd + Min)
Randomize 初始化随机数种子
然后用下面的方式生成一个在Max和Min之间的随机整数
Int((Max- Min+ 1) * Rnd + Min)
解决方案 »
- VB怎样获得鼠标处的超级连接
- vbs中关于两个时间直接想减的问题
- 我想用VB做一个类似Ctrl+C的系统级快捷键,我怎么做?
- 如何实现类似VB的 IDE(No 2)?
- vb里面“回车”是什么符号,我想把一段文本里的回车符号替换掉!
- 送分的问题(符号的问题)
- 如何读取SERVER中没有共享的文件夹(路径已知)
- 急!mshflexgrid不支持所需求的数据绑定类型
- 怎么更改程序图标
- 我刚发的贴子就给冲掉了:http://www.csdn.net/Expert/topic/476/476088.shtm 请大家来帮忙,谢谢。
- 我的问题还是CSDN的问题?
- 怎样在程序中打印DataGrid和ADO的Recordset
Randomize
必须否则,每次运行产生的随机数顺序都相同。然后,照ACTIVE 说的取界于MAX或MIN 之间的数字,如:
INT(9-0+1)*RND+0)可以取到0到9之间的随机数
Dim NumBalls% 'declare the need variables
Dim Winners%(1 To 6)
Dim Title As StringSub PredictNumbers()Dim inum%, n%, Temp%
Randomize
ReDim Balls%(1 To 49)
Dim LoopCheck%, NoSwap%Timer2.Enabled = False 'disabled the timer
For n = 0 To 5 'reset all labels and back colours
Label2(n).Caption = ""
Shape1(n).BackColor = &HC0C0C0
Next nFor n = 1 To 49: Balls%(n) = n: Next n 'set the 49 balls
For n = 1 To 6
LoopCheck = 0Do
inum = Int(49 * Rnd + 1)
Loop Until (Balls%(inum) > 0) Or (LoopCheck > 99)
Balls%(inum) = 0
Winners%(n) = inum
Next n
Do
NoSwap = True
For n = 1 To 5
If Winners%(n + 1) < Winners%(n) Then
Temp = Winners%(n + 1)
Winners%(n + 1) = Winners%(n)
Winners%(n) = Temp
NoSwap = False
End If
Next n
Loop Until NoSwap%Timer2.Enabled = True 'enable the timer
NumBalls = 1
Title = String(30, " ") + "Your chossen lotter numbers for this have been picked GOOD LUCK!" 'scroll this message
End Sub
Private Sub Cancel_Click()Unload Me 'exit the programEnd SubPrivate Sub Form_Load()Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2 'centre the form on the screenTitle = String(50, " ") + "Lottery Predictor" 'scroll this messageEnd SubPrivate Sub Predict_Click()Call PredictNumbers 'predict the numbersEnd SubPrivate Sub Timer1_Timer()
Title = Mid(Title, 2) & Left(Title, 1)
Lottery.Caption = Title
End SubPrivate Sub timer2_Timer()Dim BNum%, BCol&If NumBalls > 6 Then Exit Sub
BNum = Winners%(NumBalls)
If BNum < 10 Then BCol = &HFFFFFF
If BNum >= 10 And BNum < 20 Then BCol = &HFF8080
If BNum >= 20 And BNum < 30 Then BCol = &H8080FF
If BNum >= 30 And BNum < 40 Then BCol = &HFF00&
If BNum >= 40 Then BCol = &HFFFF& Shape1(NumBalls - 1).BackColor = BCol
Label2(NumBalls - 1).Caption = BNum
NumBalls = NumBalls + 1End Sub