Home > Enterprise >  VBA Function Question about Randomness and Shuffling
VBA Function Question about Randomness and Shuffling

Time:01-26

Good Afternoon,

I have a question about a function that I'm making.

I'm using a very old version of VBA so old that you can't even return arrays. I created a function here and I'm looking to get some clarity on how it works.

I copied most of this from another post and modified it to get the proper end result.

` x = Array(679, 680, 683, 781, 790, 792, 800, 801, 809, 818, 822, 871, 897, 911, 913, 924, 927, 929, 930, 934, 936, 946, 951, 952, 956, 970, 971)

Dim i As Long
For i = UBound(x) To LBound(x) Step -1
    Dim t As Variant
    t = x(i)
    Dim j As Long
    j = CLng((25 - 0   1) * Rnd   0)
    x(i) = x(j)
    x(j) = t
Next i`

Why is it that the numbers never duplicate with this function?

So when I looped through the outcome and the old array. It never overlaps numbers. I just wanna learn more about why it works or even how I Could make it better. The J = CLng((Max - min 1) * Rnd Min) but I just moved it the way I had it.

Why does the randomness never overlap to create duplication. I looped the outcomes like over 100k times. I'm a super novice.

I'm just looking for an understanding of this function.

CodePudding user response:

Notice the line I change to dynamically increase the chosen subindex according to array size.

Dim i As Long
For i = UBound(x) To LBound(x) Step -1
    Dim t As Variant
    t = x(i)
    Dim j As Long
    j = CLng(LBound(x) * Rnd )
    x(i) = x(j)
    x(j) = t
Next i
  •  Tags:  
  • vba
  • Related