0

All I want to do is to "scatter" the numbers from 0 to 9 inside a NumPad like the Pin Number Pads I saw on the internet. So far, this is my code:

Private Sub Command3_Click()
   For i = 0 To 9
       Command1(i).Caption = Int(10 * Rnd)
   Next i
End Sub

wherein Command1(i) is the name of each number buttons I have. Each of them has an index equal to their caption, for now. (Well, there isn't really a dependence between the caption and the index, so let's just leave it at that)

Now, as I said, I want to scatter the numbers or rearrange the numbers, and that's why I have that code.

Do you have a better suggestion on this? :/ I just can't get it. I scanned Google for "No Duplication Random numbers" thingy but I just can't get it.

Oh, in case you'll worry about the whole pin number pad program wherein you don't see any labels or text boxes or whatever, just ignore it. I just want what I'm aiming for now.

Lennart
  • 9,657
  • 16
  • 68
  • 84
  • 1
    http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle – Ňɏssa Pøngjǣrdenlarp Sep 02 '14 at 14:32
  • Oh, thanks for that Wikipedia Topic, Plutonix... finally got it!Private Sub Command3() Dim i As Integer Dim j As Integer Dim tmp As Integer Randomize For i = 0 To 9 - 1 ' Pick a random entry. j = Int((9 - i + 1) * Rnd + i) ' Swap the numbers. tmp = Command1(i).Caption Command1(i).Caption = Command1(j).Caption Command1(j).Caption = tmp Next i End Sub Private Sub Command1_Click(Index As Integer) Text1.Text = Text1.Text & Command1(Index).Caption Call Command3 End Sub – Pat-kun Teruel Sep 02 '14 at 15:10
  • there are numerous Shuffle code samples here in various languages. – Ňɏssa Pøngjǣrdenlarp Sep 02 '14 at 15:11
  • possible duplicate of [Different numbers from 1 to 10](http://stackoverflow.com/questions/1858610/different-numbers-from-1-to-10) – jac Sep 02 '14 at 21:50

0 Answers0