Results 1 to 13 of 13

Thread: [RESOLVED] dim Array(50) . array (x), random number

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    17

    Resolved [RESOLVED] dim Array(50) . array (x), random number

    I want to add number of numbers to be randomize from user not from the code
    So the code is: dim Array (1 to 50) as long
    I want to change 50 by 20 or any other no. while run time
    My idea is to put x instead of 50 and the user add 50 from text or inputbox
    Text1.text =x
    But the array(x) could not be worked, must be number
    Please help me adjust the code
    Thank you

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: dim Array(50) . array (x), random number

    Welcome to the forums.

    If you create a static array then you cannot resize it. If you create a dynamic array, then you can resize.

    Recommend spending some time in this FAQ: What are arrays and how do I use them
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    17

    Re: dim Array(50) . array (x), random number

    Dear friend
    Thank you very much for your help
    I want code for randomize numbers from 1 to x which can be chosen from the user like 20 or 50 or whatever, but without repeat the number
    Thank you again and God Bless you

  4. #4

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    17

    Re: dim Array(50) . array (x), random number

    Quote Originally Posted by MartinLiss View Post
    If the user chooses, say, 20 random numbers, what kind of random numbers do you want? 1 to 20? decimals? What are maximum and minimum?
    non-repeating Random numbers from 1 to any number of numbers chosen by user who is using the program during run time,
    the user want from 1 to 50 numbers or to 20 or to 10 numbers for examples, I want a vb6 code for that.
    Last edited by Mido; Nov 12th, 2009 at 07:11 PM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: dim Array(50) . array (x), random number

    Go LINQ, go LINQ!
    Code:
    Public Function GetRandomArray(ByVal length As Integer) As Integer()
        Dim rng As New Random
    
        Return Enumerable.Range(1, length).OrderBy(Function(i) rng.NextDouble()).ToArray()
    End Function
    Create a range of numbers from 1 to length, order them randomly and then convert the list to an array. E.g.
    Code:
    Dim arr As Integer() = GetRandomArray(20)
    
    For Each i As Integer In arr
        MessageBox.Show(i.ToString())
    Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: dim Array(50) . array (x), random number

    Quote Originally Posted by jmcilhinney View Post
    Go LINQ, go LINQ!
    Code:
    Public Function GetRandomArray(ByVal length As Integer) As Integer()
        Dim rng As New Random
    
        Return Enumerable.Range(1, length).OrderBy(Function(i) rng.NextDouble()).ToArray()
    End Function
    Create a range of numbers from 1 to length, order them randomly and then convert the list to an array. E.g.
    Code:
    Dim arr As Integer() = GetRandomArray(20)
    
    For Each i As Integer In arr
        MessageBox.Show(i.ToString())
    Next
    Guess you didn't see that this was VB6.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: dim Array(50) . array (x), random number

    Quote Originally Posted by MartinLiss View Post
    Guess you didn't see that this was VB6.
    I guess I didn't, given that I got to this thread via a link posted in another thread in the VB.NET forum. I guess I assume that noone would be silly enough to post a request for help on an existing VB6 thread in the VB.NET forum. I guess the world is always able to surprise.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: dim Array(50) . array (x), random number

    Quote Originally Posted by Mido View Post
    Random numbers from 1 to any number of numbers chosen by user who is using the program during run time,
    the user want 50 numbers or 20 or 10 numbers like 1,2,3,4,....to 20 for examples, I want a vb6 code for that.
    So if the user wants 20 numbers you just want the numbers 1 to 20 in random order?

    BTW please don't post links to this thread from this or any other forum.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    17

    Re: dim Array(50) . array (x), random number

    This is the code I have, and it is working fine,
    but the problem in it that I want the user of the program, while it is in run time to choose the number from (1 to 20) or from (1 to 30) or any other number,
    Dim myArray(1 To 52) As Long
    but because as I know now it is static array, and can't be changed in run time, can anyone make a code like the code below but with what I said before, Thanks.

    Code:
    Private Sub Form_Load()
    
      'assure random numbers don't
      'repeat each run
       Randomize
          
    End Sub
             
             
    Private Sub Command1_Click()
    
      'Set the number of elements needed. This demo
      'uses 1 to 52 to simulate a deck of cards.
       Dim cnt As Long
       Dim myArray(1 To 52) As Long
       
      'lists are for info only
       List1.Clear
       List2.Clear
       
      'fill the array with consecutive numbers from 1 to 52
       For cnt = 1 To UBound(myArray)
          myArray(cnt) = cnt
    
         'debug/info only - not needed for routine
          List1.AddItem cnt & vbTab & myArray(cnt)
       Next
       
      'randomize (suffle) the array values
       RandomizeArray myArray
       
      'debug/info only - not needed for routine
       For cnt = 1 To UBound(myArray)
          List2.AddItem cnt & vbTab & myArray(cnt)
       Next
       'this is what i need
       Text1.Text = Val(myArray(1))
       End Sub
    
    
    Private Sub RandomizeArray(ArrayIn As Variant)
       
       Dim cnt As Long
       Dim RandomIndex As Long
       Dim tmp As Variant
       
      'only if an array was passed
       If VarType(ArrayIn) >= vbArray Then
            
         'loop through the array elements in reverse
          For cnt = UBound(ArrayIn) To LBound(ArrayIn) Step -1
          
            'select a random array index
             RandomIndex = Int((cnt - LBound(ArrayIn) + 1) * _
                           Rnd + LBound(ArrayIn))
                                        
            'cnt represents one array member
            'index, and RandomIndex represents
            'another, so swap the data held in
            'myarray(cnt) with that in myarray(RandomIndex)
             tmp = ArrayIn(RandomIndex)
             ArrayIn(RandomIndex) = ArrayIn(cnt)
             ArrayIn(cnt) = tmp
             
          Next
          
       Else
       
         'The passed argument was not an
         'array; error handler goes here
          
       End If
      
    End Sub
    
    
    Private Sub List1_Scroll()
    
      'if List2 is scrolled, keep List1 in sync
       List2.TopIndex = List1.TopIndex
       
    End Sub
    
    
    Private Sub List2_Scroll()
    
      'if List1 is scrolled, keep List2 in sync
       List1.TopIndex = List2.TopIndex
    
    End Sub

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: dim Array(50) . array (x), random number

    Code:
    Option Explicit
    Option Base 1
    Private Sub Form_Load()
    
      'assure random numbers don't
      'repeat each run
       Randomize
          
    End Sub
             
             
    Private Sub Command1_Click()
    
      'Set the number of elements needed. This demo
      'uses 1 to 52 to simulate a deck of cards.
       Dim cnt As Long
       Dim myArray(0) As Long
       Dim intHowMany As Integer
       
       intHowMany = InputBox("How many")
       ReDim myArray(intHowMany)   
      'lists are for info only
       List1.Clear
       List2.Clear
       
      'fill the array with consecutive numbers from 1 to 52
       For cnt = 1 To UBound(myArray)
          myArray(cnt) = cnt
    
         'debug/info only - not needed for routine
          List1.AddItem cnt & vbTab & myArray(cnt)
       Next
       
      'randomize (suffle) the array values
       RandomizeArray myArray
       
      'debug/info only - not needed for routine
       For cnt = 1 To UBound(myArray)
          List2.AddItem cnt & vbTab & myArray(cnt)
       Next
       'this is what i need
       Text1.Text = Val(myArray(1))
       End Sub
    
    
    Private Sub RandomizeArray(ArrayIn As Variant)
       
       Dim cnt As Long
       Dim RandomIndex As Long
       Dim tmp As Variant
       
      'only if an array was passed
       If VarType(ArrayIn) >= vbArray Then
            
         'loop through the array elements in reverse
          For cnt = UBound(ArrayIn) To LBound(ArrayIn) Step -1
          
            'select a random array index
             RandomIndex = Int((cnt - LBound(ArrayIn) + 1) * _
                           Rnd + LBound(ArrayIn))
                                        
            'cnt represents one array member
            'index, and RandomIndex represents
            'another, so swap the data held in
            'myarray(cnt) with that in myarray(RandomIndex)
             tmp = ArrayIn(RandomIndex)
             ArrayIn(RandomIndex) = ArrayIn(cnt)
             ArrayIn(cnt) = tmp
             
          Next
          
       Else
       
         'The passed argument was not an
         'array; error handler goes here
          
       End If
      
    End Sub
    
    
    Private Sub List1_Scroll()
    
      'if List2 is scrolled, keep List1 in sync
       List2.TopIndex = List1.TopIndex
       
    End Sub
    
    
    Private Sub List2_Scroll()
    
      'if List1 is scrolled, keep List2 in sync
       List1.TopIndex = List2.TopIndex
    
    End Sub

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Nov 2009
    Posts
    17

    Re: dim Array(50) . array (x), random number

    I found the solution by myself

    Code:
    Private Sub Form_Load()
    
      'assure random numbers don't
      'repeat each run
       Randomize
          
    End Sub
             
             
    Private Sub Command1_Click()
    
       x = Val(Text2)
    
      'Set the number of elements needed. This demo
      'uses 1 to 52 to simulate a deck of cards.
      
       Dim cnt As Long
       ReDim myArray(1 To x) As Long
       
      'lists are for info only
       List1.Clear
       List2.Clear
       
      'fill the array with consecutive numbers from 1 to 52
       For cnt = 1 To UBound(myArray)
          myArray(cnt) = cnt
    
         'debug/info only - not needed for routine
          List1.AddItem cnt & vbTab & myArray(cnt)
       Next
       
      'randomize (suffle) the array values
       RandomizeArray myArray
       
      'debug/info only - not needed for routine
       For cnt = 1 To UBound(myArray)
          List2.AddItem cnt & vbTab & myArray(cnt)
       Next
       'this is what i need
       Text1.Text = Val(myArray(1))
       End Sub
    
    
    Private Sub RandomizeArray(ArrayIn As Variant)
       
       Dim cnt As Long
       Dim RandomIndex As Long
       Dim tmp As Variant
       
      'only if an array was passed
       If VarType(ArrayIn) >= vbArray Then
            
         'loop through the array elements in reverse
          For cnt = UBound(ArrayIn) To LBound(ArrayIn) Step -1
          
            'select a random array index
             RandomIndex = Int((cnt - LBound(ArrayIn) + 1) * _
                           Rnd + LBound(ArrayIn))
                                        
            'cnt represents one array member
            'index, and RandomIndex represents
            'another, so swap the data held in
            'myarray(cnt) with that in myarray(RandomIndex)
             tmp = ArrayIn(RandomIndex)
             ArrayIn(RandomIndex) = ArrayIn(cnt)
             ArrayIn(cnt) = tmp
             
          Next
          
       Else
       
         'The passed argument was not an
         'array; error handler goes here
          
       End If
      
    End Sub
    
    
    Private Sub List1_Scroll()
    
      'if List2 is scrolled, keep List1 in sync
       List2.TopIndex = List1.TopIndex
       
    End Sub
    
    
    Private Sub List2_Scroll()
    
      'if List1 is scrolled, keep List2 in sync
       List1.TopIndex = List2.TopIndex
    
    End Sub

  13. #13
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: dim Array(50) . array (x), random number

    to try this, add a listbox, textbox and a command button...
    Code:
    Private Sub Command1_Click()
    Dim NumNew As Long
    Dim NumExist As Long
    Dim TotalNums As Long
    Dim i As Integer, j As Long
    Dim Exist As Boolean
    
    TotalNums = Val(Text1.Text)
    Randomize
    List1.Clear 'clear listbox
    NumNew = Int(Rnd * TotalNums) + 1 'generate random number
    
    For i = 1 To TotalNums
        Do
            DoEvents
            NumNew = Int(Rnd * TotalNums) + 1 'generate random number
            For j = 0 To List1.ListCount - 1
                NumExist = List1.List(j)
                    
                If NumNew = NumExist Then
                    Exist = True
                    Exit For
                Else
                    Exist = False
                End If
                    
            Next
                
            If Exist = False Then
                List1.AddItem NumNew
            End If
                
        Loop Until Exist = False
    Next
    End Sub
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width