hello everyone. i need to check if random number generated each time button clicked have not been generated before. i'm saving random number to array each time button clicked then i need to check if after second click of the button numbers are different if not then generate new random number compare it and if it is not the same as other numbers that have been generated before, then pass it to other function.
heres what i've tried:

' Dimension the variables used in the programme

 Dim intNumber As Integer




Dim randGenerator As New Random




 Dim arrNumber(100) As String


Dim x As Integer = 0

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'generating random number

intNumber = randGenerator.Next(0, 10)

    'saving number generated to a string!

 arrNumber(x) = intNumber
x += 1


    'checking if number genareted have not been generatedd before


 For i = 0 To arrNumber.Length - 1

            If arrNumber(i) = intNumber Then

                MsgBox("number have been generated before!")

                intNumber = randGenerator.Next(0, 10)

            End If

 Next



    'rest of the code
End Sub

The problem is you are searching the array for the number you just generated:

If arrNumber(i) = intNumber Then

You made arrNumber(x) = intNumber so of course it will be there.

Try This:

Dim intNumber As Integer
        Dim randGenerator As New Random
        Dim arrNumber(100) As String
        Dim x As Integer = 0

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'generating random number
            intNumber = randGenerator.Next(0, 10)
            'checking if number genareted have not been generatedd before
repeat:
            For i = 0 To arrNumber.Length - 1
                    If arrNumber(i) = intNumber Then
                    MsgBox("number have been generated before!")
                    intNumber = randGenerator.Next(0, 10)
                    GoTo repeat
                End If
            Next
            'saving number generated to a string!
            arrNumber(x) = intNumber
            x += 1
        End Sub

This will ensure that no number is the same. Note: After 9 numbers there will be an infinite amount of message boxes. Also please when inserting code, don't include that many new lines (blank lines).

commented: thanks a lot this code worked. bout the lines(its CODE button does that) i'm new here and dont understrand the syntax of how to post code +0

Depending on how many random numbers you need and over what range, regenerating when you get a duplicate might not be a good idea. For example, if you need ten random numbers from 1-1000 there won't be a problem. However, if you need 1000 random numbers (with no duplicates) then you will be ok at the start but not at the end because the more numbers you get, the more likely it is you will get a duplicate. At the end you will spend most of your time regenerating. For a reasonably sized range you can generate an initial list containing all of the possible numbers, then remove each number from the list as needed at random, of course). Skeleton code (with no error checking) is

Imports System.Random

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim myrand As New Unique(-5, 20)
        Dim n As Integer

        Do While myrand.More()
            n = myrand.GetNext()
            Debug.WriteLine(n)
        Loop

    End Sub

End Class

Public Class Unique

    Private _list As New List(Of Integer)
    Private _rand As New Random(Now.Ticks Mod Integer.MaxValue)

    Public Sub New(lo As Integer, hi As Integer)

        For i As Integer = lo To hi
            _list.Add(i)
        Next

    End Sub

    Public Function GetNext() As Integer

        If _list.Count > 0 Then
            Dim r As Integer = _list(_rand.Next(0, _list.Count))
            _list.Remove(r)
            Return r
        Else
            Return -1
        End If

    End Function

    Public Function More() As Boolean
        Return _list.Count > 0
    End Function

End Class

_rand.Next(0, _list.Count) generates a random index into the list. So the sequence is

  • pick a random number from the list
  • remove that number from the list

Possible error checks to add would be to ensure that lo is < hi.

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.