《今天11点揭贴。希望11点前能够出现救世主》这 几天接触到VB。做了个简单的游戏,现在碰到个小麻烦。关于随机函数的问题。希望大家能帮

baizhongri 2003-12-18 09:12:59
Private Sub Command1_Click()
Dim i As Integer

Dim a(9) As Integer
For i = 1 To 9

a(i) = Int((Rnd * 9) + 1)

Print a(i)

Next i
End Sub
这个随机函数的范围是1到9,而我呢 想让它产生1到9 不重复的数。很简单的,只是刚学VB不适应 ,希望你们都能帮我。这个帖子今天11:00揭贴。。。谁给 详细的原代码,给50分。
...全文
26 点赞 收藏 12
写回复
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
SmileSkyNet 2003-12-18
你的这个应用我原来用过(我用的是0-9),我用来做猜数游戏,每次随机出现4个不同的数字组成一个四位数。。。。源码吗,我要找找,写起来有点麻烦!!!
回复
lgs666 2003-12-18
Option Explicit

Private Sub Command1_Click()
Dim I As Integer
Dim intArray(10) As Integer
Dim intRnd As Integer
Dim dblK As Double

Randomize
Do While True
intRnd = Val(Left(Rnd, 1))
If Not IsInArray(intRnd, intArray) Then
intArray(I) = intRnd
Debug.Print intArray(I)
I = I + 1
End If
If I = 9 Then
Exit Do
End If
Loop

End Sub

Private Function IsInArray(ByVal intItemToFind As Integer, ByRef intArray() As Integer) As Boolean
Dim I As Integer

For I = LBound(intArray) To UBound(intArray())
If intArray(I) = intItemToFind Then
IsInArray = True
Exit Function
End If
Next
IsInArray = False
End Function

回复
landongfang 2003-12-18
Private a(1 To 9) As Integer
Private Sub Command1_Click()

If Timer1.Enabled = True Then
Timer1.Enabled = True
Else
Timer1.Enabled = True
End If

End Sub
Private Sub Rep()
Dim i As Integer
Dim temp As Integer
Dim temp1 As Integer

temp = Int((Rnd(3) * 9) + 1)
temp1 = a(1)
a(1) = a(temp)
a(temp) = temp1
End Sub

Private Sub Form_Load()
Dim i As Integer
For i = 1 To 9
a(i) = i
Next
End Sub

Private Sub Timer1_Timer()
Call Rep
End Sub
回复
hcj2002 2003-12-18
0--9不重复的数???

如果有产生10个数呢??
回复
liyan010 2003-12-18
用我这个代码吧,不需要判断是否重复,而且也不需要定义动态数组,绝对节省系统资源:

Dim mysz(8) As Integer'9维数组

Private Sub Form_Load()
Dim i As Integer
For i = 0 To 8
mysz(i) = i + 1初始化
Next i
End Sub

Private Sub Command1_Click()
Dim A As Integer, B As Integer, t As Integer, num As Integer
Dim str As String
num = 0
For num = 0 To 49
Randomize Timer
A = Int(Rnd() * 9)
Randomize Timer
B = Int(Rnd() * 9)
t = mysz(A): mysz(A) = mysz(B): mysz(B) = t'交换
Next num
For t = 0 To 8
str = str & mysz(t)
Next t
Print str'打印
End Sub
回复
SoHo_Andy 2003-12-18
试试看,我的程序

Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim a() As Integer
Dim intTemp As Integer
ReDim a(0)
For i = 1 To 9
Do While True
intTemp = Int((Rnd * 9) + 1)
For j = 0 To UBound(a)
If a(j) = intTemp Then
i = i - 1
Exit Do
End If
Next
ReDim Preserve a(i - 1)
a(i - 1) = intTemp
Debug.Print a(i - 1)
Exit Do
Loop
Next i
End Sub
回复
landongfang 2003-12-18
Private Sub Command1_Click()

Dim a(1 To 9) As Integer
Dim temp As Integer
Dim i As Integer

i = 1
While i <= 9
temp = Int((Rnd(3) * 9) + 1)
If Rep(a, temp, i) = True Then
a(i) = temp
i = i + 1
End If
Wend

End Sub
Private Function Rep(b() As Integer, T As Integer, n As Integer) As Boolean
Dim i As Integer
For i = 1 To n
If b(i) = T Then
Rep = False
Exit Function
End If
Next
Rep = True
End Function
回复
ides 2003-12-18
请再定义一个数组,如b(9),在a(i) = Int((Rnd * 9) + 1)后加上一个循环语句和一个判断语句:当每产生一个随机树就把他放到b(j)中,并要与b(j)中的每个树比较,如果产生的树在b(j)中已经有了则不再显示
回复
jhzhou882 2003-12-18
Private Sub Command1_Click()

Dim i, j As Integer
Dim use As Integer
Dim a(9) As Integer

For i = 1 To 9
use = Int((Rnd(1) * 9) + 1)
For j = 0 To i - 1
If a(j) = use Then
i = i - 1
Exit For
Else
a(i) = use
End If
Next j
Next i

End Sub
回复
liyan010 2003-12-18
我就是救世主,传说中的the one!

把1~9放在一个数组里mysz(8),然后每次随机取两个数交换数值,如第一次随机取到mysz(6)和mysz(3),则利用t=mysz(6),mysz(6)=mysz(3),mysz(3)=t来实现交换......
如此循环50次,1-9的九个数字的顺序即被随即打乱.
现在你只需从mysz(0)一直取到mysz(8)即可以随机顺序取得1-9间的随机数,而且不重复.
回复
landongfang 2003-12-18
Private Sub Command1_Click()

Dim a(1 To 9) As Integer
Dim temp As Integer
Dim i As Integer

i = 1
While i < 9
temp = Int((Rnd * 9) + 1)
If Rep(a, temp, i) = True Then
a(i) = temp
i = i + 1
End If
Print a(i)
Wend

End Sub
Private Function Rep(b() As Integer, T As Integer, n As Integer) As Boolean
Dim i As Integer
For i = 1 To n
If b(i) = T Then
Rep = False
Exit Function
End If
Next
Rep = True
End Function

回复
佛的光辉 2003-12-18
9个不重复的数?范围是多少
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7451

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告