Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » General

Post new topic   Reply to topic
Unique Random No,
View previous topic :: View next topic  
Author Message
shminhas
Newbie


Joined: 13 Jan 2004
Posts: 4

PostPosted: Jan 16th, 2004 07:39 AM    Post subject: Unique Random No, Reply with quote

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
View user's profile Send private message Send e-mail
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Jan 16th, 2004 06:16 PM    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » General All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
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


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic