Making an array more efficient-VBForums
Results 1 to 6 of 6

Thread: Making an array more efficient

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Posts
    17

    Making an array more efficient

    Hi everyone!

    In my program, I have code that I'm looking to run more efficiently.

    It looks like this:

    Code:
    Private Sub generate_random_sequence_12()
    
    Randomize
    
    random_number_array(1) = 0
    random_number_array(2) = 0
    random_number_array(3) = 0
    random_number_array(4) = 0
    random_number_array(5) = 0
    random_number_array(6) = 0
    random_number_array(7) = 0
    random_number_array(8) = 0
    random_number_array(9) = 0
    random_number_array(10) = 0
    random_number_array(11) = 0
    random_number_array(12) = 0
    
    
    Dim count
    
    For count = 1 To 12
    
    Do
        random_number = Int(12 * Rnd) + 1
    Loop While random_number = random_number_array(1) _
    Or random_number = random_number_array(2) Or random_number = random_number_array(3) _
    Or random_number = random_number_array(4) Or random_number = random_number_array(5) _
    Or random_number = random_number_array(6) Or random_number = random_number_array(7) _
    Or random_number = random_number_array(8) Or random_number = random_number_array(9) _
    Or random_number = random_number_array(10) Or random_number = random_number_array(11) _
    Or random_number = random_number_array(12)
        random_number_array(count) = random_number
    
    Next count
    
    
    End Sub
    This was fine before. However, I now want to make a version with 72 members in the array and end up with something that looks like this:

    Code:
    random_number_array(1) = 0
    random_number_array(2) = 0
    random_number_array(3) = 0
    random_number_array(4) = 0
    random_number_array(5) = 0
    random_number_array(6) = 0
    random_number_array(7) = 0
    random_number_array(8) = 0
    random_number_array(9) = 0
    random_number_array(10) = 0
    random_number_array(11) = 0
    random_number_array(12) = 0
    random_number_array(13) = 0
    random_number_array(14) = 0
    random_number_array(15) = 0
    random_number_array(16) = 0
    random_number_array(17) = 0
    random_number_array(18) = 0
    random_number_array(19) = 0
    random_number_array(20) = 0
    random_number_array(21) = 0
    random_number_array(23) = 0
    random_number_array(24) = 0
    random_number_array(25) = 0
    random_number_array(26) = 0
    random_number_array(27) = 0
    random_number_array(28) = 0
    random_number_array(29) = 0
    random_number_array(30) = 0
    random_number_array(31) = 0
    random_number_array(32) = 0
    random_number_array(33) = 0
    random_number_array(34) = 0
    random_number_array(35) = 0
    random_number_array(36) = 0
    random_number_array(37) = 0
    random_number_array(38) = 0
    random_number_array(39) = 0
    random_number_array(40) = 0
    random_number_array(41) = 0
    random_number_array(42) = 0
    random_number_array(43) = 0
    random_number_array(44) = 0
    random_number_array(45) = 0
    random_number_array(46) = 0
    random_number_array(47) = 0
    random_number_array(48) = 0
    random_number_array(49) = 0
    random_number_array(50) = 0
    random_number_array(51) = 0
    random_number_array(52) = 0
    random_number_array(53) = 0
    random_number_array(54) = 0
    random_number_array(55) = 0
    random_number_array(56) = 0
    random_number_array(57) = 0
    random_number_array(58) = 0
    random_number_array(59) = 0
    random_number_array(60) = 0
    random_number_array(61) = 0
    random_number_array(62) = 0
    random_number_array(63) = 0
    random_number_array(64) = 0
    random_number_array(65) = 0
    random_number_array(66) = 0
    random_number_array(67) = 0
    random_number_array(68) = 0
    random_number_array(69) = 0
    random_number_array(70) = 0
    random_number_array(71) = 0
    random_number_array(72) = 0
    Is there a way of cutting this down to two or three lines?

    Any help would be much appreciated!

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,868

    Re: Making an array more efficient

    You mean like using a For loop?

    Oops, it seems you want to do more than zero each element..

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,868

    Re: Making an array more efficient

    Sounds like you want a shuffle. Do a search here, we've covered that many times.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Posts
    17

    Re: Making an array more efficient

    It's an array that will be used to present a series of sub-routines in quasi-random order. In other words, I want each to be randomly selected (corresponding to a number in the array) once only. Then when the array is consulted again, the same number won't be selected again. A "shuffle"? I have never heard of that. I'll look it up, thanks!

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    17,868

    Re: Making an array more efficient

    The concept is similar to shuffling cards. You want a randomized order of a fixed set of unique values, i.e. each value occurring only once.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2014
    Posts
    17

    Re: Making an array more efficient

    Yes, that's exactly it. Sorry if the question has been asked before, I'm new to programming (it's not my field) and I wouldn't have known where to look.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.