Page 1 of 1

## Non-Repeating Random Numbers Or anything you want to use. Rate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=17139&amp;s=381e27a3c1c9088c2d3f9551f134944c&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Graham

Reputation: 3
• Posts: 49
• Joined: 07-June 06

Posted 10 July 2006 - 11:58 AM

Here is a simple tutorial to help pick six non-repeating numbers or any non-repeating elements you want to use. i.e. the Alphabet.

```	Dim intNumber As Integer
Dim arrNumber(0 To 5) As Integer
Dim i, x, y As Integer

```

There are only two controls needed on the form one is a Button and the other is a Label.
In the Buttons Event Procedure.is where all the code is placed to pick the random numbers and display the results in the Label.
First make sure that the Label is empty.

```Label1.Text = ""

```

Now we need to set up a For / Next Loop to hold six numbers,

```For x = 0 To 5
Next x

```

So, our For / Next Loop will go around six times, but without some more code it will not be very interesting.

What follows next is the code that will do the magic.
You will notice that inside our For / Next x Loop we have a nested For / Next y Loop.
This is the Loop that checks if the Random number has already been picked.
How? Well, first the random number is checked to see if it exists in our arrNumber(y), remember the tutorials on Arrays? So it looks at the first place in the array which first time the loop is entered is (0) in fact this is what the array looks like to start with.
arrNumber(0) = 0
arrNumber(1) = 0
etc
arrNumber(5) = 0
After the y Loop has finished and no numbers in the arrNumber have been found it moves on to the last bit of code inside the x For / Next Loop so that we get, assuming our first Random Number was 19 the following
arrNumber(0) = 19 once this is done off it goes again
First y Loops to see if the second Random Number has been already been picked.
Array y now looks like this
arrNumber(0) = 19
arrNumber(1) = 0
etc
arrNumber(5) = 0

If the second Random Number is 38 then as it Loops through the y Array it will not find 38, so it moves on and places 38 in the x Array (Not Medical!)
Off it goes again this time let’s say the Random Number is 19 again, as the y Loop is performed it finds 19 in arrNumber (0) position so now it uses GoTo and jumps up to Start: where it picks another Random Number to try, it keeps doing this until all six places in the Array are filled with Non-Repeating Numbers.
See the code below.

```	   For x = 0 To 5
Start:
Randomize()
intNumber = Int((49 * Rnd()) + 1
For y = 0 To 5
If intNumber = arrNumber(y) Then
GoTo Start
End If
Next y

arrNumber(x) = intNumber
Next x

```

The last stage is to place the numbers in to the Label

```		For i = 0 To 5
Label1.Text = Label1.Text & (arrNumber(i)) & " , "
Next

```

The full code is here for you to Copy (Ctrl+C) and Paste (Ctrl+V)

```Public Class Form1
' Dimension the variables used in the programme
Dim intNumber As Integer
Dim arrNumber(0 To 5) As Integer
Dim i, x, y As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Make sure the Label is clear
Label1.Text = ""

'We want a total of 6 Numbers (UK Lottery)

For x = 0 To 5
Start:
Randomize()
intNumber = Int((49 * Rnd()) + 1) ' Random number 1 to 49
For y = 0 To 5
' Check arrNumber (y)
'If intnumber has already been selected,
'Then go and select another one.
If intNumber = arrNumber(y) Then
GoTo Start
End If
Next y

'Place the next non-repeated number in the arrNumber(x).
arrNumber(x) = intNumber

Next x
'----------------------------------------------------
For i = 0 To 5
Label1.Text = Label1.Text & (arrNumber(i)) & " , "
Next

End Sub

End Class

```

Is This A Good Question/Topic? 3

## Replies To: Non-Repeating Random Numbers

### #2 5ubw0r1d

Reputation: 2
• Posts: 78
• Joined: 18-May 07

Posted 11 June 2007 - 03:15 AM

Nice tutorial, well presented. However, the goto command is a bit old-school and frowned upon by many nowadays.

### #3 westmatrix99

Reputation: 0
• Posts: 20
• Joined: 29-September 07

Posted 29 September 2007 - 03:50 PM

Not working for me, are you sure you created this in VB or VB6?

This post has been edited by westmatrix99: 29 September 2007 - 03:52 PM

### #4 akhileshbc

Reputation: 9
• Posts: 179
• Joined: 27-September 08

Posted 27 September 2008 - 10:33 PM

westmatrix99, on 29 Sep, 2007 - 03:50 PM, said:

Not working for me, are you sure you created this in VB or VB6?

I think its .net

### #5 biggles2008

• Bassface

Reputation: 9
• Posts: 623
• Joined: 05-March 08

Posted 30 April 2009 - 03:52 PM

Yes this is .NET

"Label1.Text" = VB.NET

"Label1.Caption" = VB6

### #6 crepitus

• D.I.C Regular

Reputation: 85
• Posts: 383
• Joined: 08-September 09

Posted 22 October 2009 - 01:51 AM

A better algorithm is to place all the possible picks into a collection. Then randomly choose an index in the collection to make a selection. Then remove that item from the collection so that it cannot be selected again. This means it won't need to repeatedly try to pick an item it hasn't selected before.

```Module Module1

Private rand As New Random

Sub Main()
' Store the numbers 1 to 6 in a list '
Dim allNumbers As New List(Of Integer)(Enumerable.Range(1, 6))
' Store the randomly selected numbers in this list: '
Dim selectedNumbers As New List(Of Integer)
For i As Integer = 0 To 5
' A random index in numbers '
Dim index As Integer = rand.Next(0, allNumbers.Count)
' Copy the item at index from allNumbers. '
Dim selectedNumber As Integer = allNumbers(index)
' And store it in our list of picked numbers. '
' Remove the item from the list so that it cannot be picked again. '
allNumbers.RemoveAt(index)
Next
' Show them on the command line '
For Each i As Integer In selectedNumbers
Console.WriteLine(i)
Next
End Sub

End Module
```

### #7 toyamah

Reputation: 0
• Posts: 1
• Joined: 15-June 09

Posted 21 March 2012 - 06:59 PM

could you further explain this section

intNumber = Int((49 * Rnd()) + 1) ' Random number 1 to 49

Im wondering why is there + 1. What's the purpose?

Thanks you.

### #8 normanbates

Reputation: 0
• Posts: 1
• Joined: 30-March 12

Posted 30 March 2012 - 06:51 AM

toyamah, on 21 March 2012 - 06:59 PM, said:

could you further explain this section

intNumber = Int((49 * Rnd()) + 1) ' Random number 1 to 49

Im wondering why is there + 1. What's the purpose?

Thanks you.

It stops it from choosing 0 by + 1 to any of the numbers it selects

### #9 PNJLj

Reputation: 6
• Posts: 237
• Joined: 18-May 09

Posted 25 May 2012 - 05:27 AM

how would you implement this to using the Alphabet? i cannot figure it out. thanks for any replies

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

Posted 25 May 2012 - 06:01 AM

PNJLj, on 25 May 2012 - 01:27 PM, said:

how would you implement this to using the Alphabet? i cannot figure it out. thanks for any replies

What's a string? Could consider it a collection of Char? Snippet
If that doesn't help, then you need think of a real world analogy. I use a Bag of Shopping.

Reputation: 0
• Posts: 2
• Joined: 16-September 12

Posted 16 September 2012 - 01:46 PM

thanks the code really works, what if i want the result to be in ascending order

Reputation: 0
• Posts: 2
• Joined: 16-September 12

Posted 17 September 2012 - 01:22 AM

very helpful topic, i figure it out how to arrange then in ascending order using:
```array.sort(arrNumber)
```

thanks graham

### #13 mauro514

Reputation: 0
• Posts: 1
• Joined: 08-February 13

Posted 08 February 2013 - 11:53 PM

Thanks so much! This is exactly what I was looking for.

### #14 sony_z

Reputation: 0
• Posts: 4
• Joined: 14-March 14

Posted 14 March 2014 - 12:22 AM

I am new here, and I have just discovered this site and this code is exactly what I needed. However, I have not tried the code yet(I'm in school), but I want to ask what is happening after the cycle has gone trough all the numbers. And I wonder if I would be able to put a text (something like this, I'm working on my own music game) - "you have reached the end of this level, select the next one"...?
(P.s. - sorry if my english is bad)

### #15 andrewsw

• blow up my boots

Reputation: 6545
• Posts: 26,533
• Joined: 12-December 12

Posted 01 November 2015 - 03:04 PM

The Random Class should be used in preference to the older (VB6) Randomize and Rnd() approach. Using GoTo is also unnecessary and should be avoided. (Randomize also only needs to be called once, not repeatedly within a loop.)

Post #6 demonstrates a better approach that removes items from a list (rather than using nested loops). We can also take advantage of the fact that an array of booleans defaults to false. We can use an array of 49 False values and tick of (set to True) each value as it is chosen.
```Public Class Form1
Private rand As Random = New Random()
Private randoms(5) As Integer

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim bUsed(49) As Boolean        'elements default to False
Dim iTempRand As Integer

For x As Integer = 0 To 5
Do
iTempRand = rand.Next(1, 50)        'maxValue is exclusive
Loop Until bUsed(iTempRand) = False
randoms(x) = iTempRand
bUsed(iTempRand) = True                 'we've used this number
Next
Label1.Text = String.Join(",", randoms).TrimEnd(",")

End Sub
End Class
```

I am also using String.Join to concatenate all the values in one go, rather than repeatedly changing the (UI) Text property of the Label.

This post has been edited by andrewsw: 01 November 2015 - 03:05 PM