Welcome to Dream.In.Code
Getting Help is Easy!

Join 131,772 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,101 people online right now. Registration is fast and FREE... Join Now!




Non-Repeating Random Numbers

 
Reply to this topicStart new topic

> Non-Repeating Random Numbers, Or anything you want to use.

Graham
Group Icon



post 10 Jul, 2006 - 10:58 AM
Post #1


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.
As always we start with the Dim statements

CODE


    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.

CODE

Label1.Text = ""


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

CODE

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.

CODE

       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

CODE

        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)

CODE


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


Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

5ubw0r1d
Group Icon



post 11 Jun, 2007 - 02:15 AM
Post #2
Nice tutorial, well presented. However, the goto command is a bit old-school and frowned upon by many nowadays.

Go to the top of the page
+Quote Post

westmatrix99
*



post 29 Sep, 2007 - 02:50 PM
Post #3
Not working for me, are you sure you created this in VB or VB6?

This post has been edited by westmatrix99: 29 Sep, 2007 - 02:52 PM
Go to the top of the page
+Quote Post

akhileshbc
Group Icon



post 27 Sep, 2008 - 09:33 PM
Post #4
QUOTE(westmatrix99 @ 29 Sep, 2007 - 03:50 PM) *

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

I think its .net
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 11/20/08 01:53PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month