Hello, I am trying to make a random number generator (its not really a random number generator, more a random order generator) that puts the numbers 1, 14 in a random order, without duplicating the numbers. That is the problem I am having it will often select the same number once or twice (presumably because of the limited range)

My tutor has suggested to do it using an array, and 2 loops however, I cannot seem to find a way of doing that, so I have attempted it my own way, though no luck.

If anyone can point me in the right direction I would be very grateful.

Here is my code

Public Class Stations
    Dim RDN As Random
    Dim total As Integer
    Dim Value As Integer
    Dim stid1, stid2, stid3, stid4, stid5, stid6, stid7, stid8, stid9, stid10, stid11, stid12, stid13, stid14 As Integer


    Private Sub Stations_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        RDN = New Random
        Debug.Text = 0
        tbtotal.Text = 0

    End Sub


    Private Sub bRandom_Click(sender As System.Object, e As System.EventArgs) Handles bRandom.Click
        stid1 = RDN.Next(1, 15)
        stid2 = RDN.Next(1, 15)
        stid3 = RDN.Next(1, 15)
        stid4 = RDN.Next(1, 15)
        stid5 = RDN.Next(1, 15)
        stid6 = RDN.Next(1, 15)
        stid7 = RDN.Next(1, 15)
        stid8 = RDN.Next(1, 15)
        stid9 = RDN.Next(1, 15)
        stid10 = RDN.Next(1, 15)
        stid11 = RDN.Next(1, 15)
        stid12 = RDN.Next(1, 15)
        stid13 = RDN.Next(1, 15)
        stid14 = RDN.Next(1, 15)

        tbstid1.Text = stid1.ToString
        tbstid2.Text = stid2.ToString
        tbstid3.Text = stid3.ToString
        tbstid4.Text = stid4.ToString
        tbstid5.Text = stid5.ToString
        tbstid6.Text = stid6.ToString
        tbstid7.Text = stid7.ToString
        tbstid8.Text = stid8.ToString
        tbstid9.Text = stid9.ToString
        tbstid10.Text = stid10.ToString
        tbstid11.Text = stid11.ToString
        tbstid12.Text = stid12.ToString
        tbstid13.Text = stid13.ToString
        tbstid14.Text = stid14.ToString
        Do While tbtotal.Text <> total
            If tbstid2.Text = tbstid1.Text Then
                tbstid2.Clear()
                tbstid2.Text = stid2
            ElseIf tbstid3.Text = tbstid2.Text Or tbstid1.Text Then
                tbstid3.Clear()
                tbstid3.Text = stid3
            ElseIf tbstid4.Text = tbstid3.Text Or tbstid2.Text Or tbstid1.Text Then
                tbstid4.Clear()
                tbstid4.Text = stid4
            ElseIf tbstid5.Text = tbstid4.Text Or tbstid3.Text Or tbstid2.Text Or tbstid1.Text Then
                tbstid5.Clear()
                tbstid5.Text = stid5
            ElseIf tbstid6.Text = tbstid5.Text Or tbstid4.Text Or tbstid3.Text Or tbstid2.Text Or tbstid1.Text Then
                tbstid6.Clear()
                tbstid6.Text = stid6
            ElseIf tbstid7.Text = tbstid6.Text Or tbstid5.Text Or tbstid4.Text Or tbstid3.Text Or tbstid2.Text Or tbstid1.Text Then
                tbstid7.Clear()
                tbstid7.Text = stid7
            ElseIf tbstid8.Text = tbstid7.Text Or tbstid6.Text Or tbstid5.Text Or tbstid4.Text Or tbstid3.Text Or tbstid2.Text Or tbstid1.Text Then
                tbstid8.Clear()
                tbstid8.Text = stid8
            ElseIf tbstid9.Text = tbstid8.Text Or tbstid7.Text Or tbstid6.Text Or tbstid5.Text Or tbstid4.Text Or tbstid3.Text Or tbstid2.Text Or tbstid1.Text Then
                tbstid9.Clear()
                tbstid9.Text = stid9
            ElseIf tbstid10.Text = tbstid9.Text Or tbstid8.Text Or tbstid7.Text Or tbstid6.Text Or tbstid5.Text Or tbstid4.Text Or tbstid3.Text Or tbstid2.Text Or tbstid1.Text Then
                tbstid10.Clear()
                tbstid10.Text = stid10
            ElseIf tbstid11.Text = tbstid10.Text Or tbstid9.Text Or tbstid8.Text Or tbstid7.Text Or tbstid6.Text Or tbstid5.Text Or tbstid4.Text Or tbstid3.Text Or tbstid2.Text Or tbstid1.Text Then
                tbstid11.Clear()
                tbstid11.Text = stid11
            ElseIf tbstid12.Text = tbstid11.Text Or tbstid10.Text Or tbstid9.Text Or tbstid8.Text Or tbstid7.Text Or tbstid6.Text Or tbstid5.Text Or tbstid4.Text Or tbstid3.Text Or tbstid2.Text Or tbstid1.Text Then
                tbstid12.Clear()
                tbstid12.Text = stid12
            ElseIf tbstid13.Text = tbstid12.Text Or tbstid11.Text Or tbstid10.Text Or tbstid9.Text Or tbstid8.Text Or tbstid7.Text Or tbstid6.Text Or tbstid5.Text Or tbstid4.Text Or tbstid3.Text Or tbstid2.Text Or tbstid1.Text Then
                tbstid13.Clear()
                tbstid13.Text = stid13
            ElseIf tbstid14.Text = tbstid13.Text Or tbstid12.Text Or tbstid11.Text Or tbstid10.Text Or tbstid9.Text Or tbstid8.Text Or tbstid7.Text Or tbstid6.Text Or tbstid5.Text Or tbstid4.Text Or tbstid3.Text Or tbstid2.Text Or tbstid1.Text Then
                tbstid14.Clear()
                tbstid14.Text = stid14

            End If
        Loop

    End Sub 
End Class

Thank you in advance.

Dani AI

Generated

A clean, reliable way to get the numbers 1..14 in a random order (no duplicates) is to build the list of values and shuffle it once, then write the shuffled values into the TextBox controls. Generating 14 independent random calls and trying to patch duplicates with many Ifs is fragile and error-prone. The single-list + shuffle approach guarantees every number appears exactly once and is straightforward to reason about. 's control-array suggestion is useful here: keep the TextBoxes in a single array and fill them from the shuffled list.

' Fisher-Yates shuffle helper
Private Sub Shuffle(Of T)(list As IList(Of T), rng As Random)
    For i As Integer = list.Count - 1 To 1 Step -1
        Dim j As Integer = rng.Next(i + 1)    ' 0 .. i inclusive
        Dim tmp As T = list(i)
        list(i) = list(j)
        list(j) = tmp
    Next
End Sub

' Example usage (e.g. inside the button click)
Dim nums As New List(Of Integer)
For i As Integer = 1 To 14
    nums.Add(i)
Next

Shuffle(nums, RDN)   ' RDN should be a single Random instance created once (Form_Load)

Dim boxes = New TextBox() {tbstid1, tbstid2, tbstid3, tbstid4, tbstid5, tbstid6, tbstid7, tbstid8, tbstid9, tbstid10, tbstid11, tbstid12, tbstid13, tbstid14}
For i As Integer = 0 To nums.Count - 1
    boxes(i).Text = nums(i).ToString()
Next

Notes and troubleshooting: keep one Random instance (created once on load) — creating many Random objects in quick succession can reuse the same seed and produce repeated values. The Fisher-Yates method implemented above produces an unbiased permutation when the swap index is chosen with rng.Next(i + 1). Using LINQ OrderBy(Function(x) rng.Next()) is shorter but can be less efficient and may introduce subtle bias; prefer Fisher-Yates for correctness. If duplicates still appear, check for accidental re-use of old values, race conditions from multiple clicks, or mishandling of the TextBox array.

in the first instance create a control array of text boxes, put one text box on the form, copy and paste it, VB will ask if you want to create a control array to which you answer Yes. Then past the remaining 13 text boxes.

randomise
for i = 1 to 14
  textboxNo = int(rnd()*14)
  do until textbox(textboxNo).text = ""
     textboxNo = int(rnd()*14)
  loop
  textbox(TextboxNo).text = i
next i
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.