 |
| View previous topic :: View next topic |
| Author |
Message |
shminhas Newbie
Joined: 13 Jan 2004 Posts: 4
|
Posted: Jan 16th, 2004 07:39 AM Post subject: Unique Random No, |
|
|
I am facing a problem.....which is:
I have an array of 10 elements, I want to store random numbers from 1 to 100 in it but the random number should be unique.
Thanx in advance |
|
| Back to top |
|
P.T.A.M. Administrator

Joined: 08 Oct 2003 Posts: 752 Location: Greece
|
Posted: Jan 16th, 2004 06:16 PM Post subject: |
|
|
This does it :
| Code: | Option Explicit
Private Sub Form_Load()
Dim MyArray(1 To 10) As Integer
Dim i As Integer
Dim R As Integer
Dim B As Boolean
For i = 1 To 10
Do
R = Int(Rnd * 100) + 1
B = Exists(MyArray, R)
If B = False Then
MyArray(i) = R
End If
Loop Until B = False
Next
Dim S As String
For i = 1 To 10
S = S & MyArray(i) & " "
Next
MsgBox S
End Sub
Private Function Exists(Ar() As Integer, V As Integer) As Boolean
Dim i As Integer
For i = LBound(Ar) To UBound(Ar)
If Ar(i) = V Then
Exists = True
End If
Next
End Function |
Also have a look at this : http://www.developerkb.com/modules/wfsection/article.php?articleid=29 _________________ No one is completely useless. They can at least be an example of what to avoid. |
|
| Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|