Generate random numbers without repeat using C# and VB.Net in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

Hello Forum, 

With the use of the code below, will there be possibility that the random number will be repeated?

If so, how can I guard against auto generated to repeat itself?

        if (!IsPostBack)
        {
            string numbers = "1234567890";

            string characters = numbers;
            int length = 7;
            string id = string.Empty;
            for (int i = 0; i < length; i++)
            {
                string character = string.Empty;
                do
                {
                    int index = new Random().Next(0, characters.Length);
                    character = characters.ToCharArray()[index].ToString();
                } while (id.IndexOf(character) != -1);
                id += character;
            }
            Labelreceipt.Text = id;
        }

 

Posted one month ago

Hi George616,

To avoid repeat number save the generated number as list in session and check the list while generating new number.

Once the work done clear the Session.

Refer below code. 

HTML

<asp:Button Text="Generate Number" runat="server" OnClick="OnDistinctNumber" />
<hr />
<asp:Label ID="lblNumber" runat="server" />

Code

C#

protected void OnDistinctNumber(object sender, EventArgs e)
{
    List<string> result = new List<string>();
    if (Session["Numbers"] != null)
    {
        result = (List<string>)Session["Numbers"];
    }
    string number = GetAutoNumber();
    if (!result.Contains(number))
    {
        result.Add(number);
        Session["Numbers"] = result;
        lblNumber.Text = number;
    }
}

private string GetAutoNumber()
{
    string numbers = "1234567890";
    string characters = numbers;
    int length = 7;
    string id = string.Empty;
    for (int i = 0; i < length; i++)
    {
        string character = string.Empty;
        do
        {
            int index = new Random().Next(0, characters.Length);
            character = characters.ToCharArray()[index].ToString();
        } while (id.IndexOf(character) != -1);
        id += character;
    }

    return id;
}

VB.Net

Protected Sub OnDistinctNumber(ByVal sender As Object, ByVal e As EventArgs)
    Dim result As List(Of String) = New List(Of String)()
    If Session("Numbers") IsNot Nothing Then
        result = CType(Session("Numbers"), List(Of String))
    End If
    Dim number As String = GetAutoNumber()
    If Not result.Contains(number) Then
        result.Add(number)
        Session("Numbers") = result
        lblNumber.Text = number
    End If
End Sub

Private Function GetAutoNumber() As String
    Dim numbers As String = "1234567890"
    Dim characters As String = numbers
    Dim length As Integer = 7
    Dim id As String = String.Empty
    For i As Integer = 0 To length - 1
        Dim character As String = String.Empty
        Do
            Dim index As Integer = New Random().Next(0, characters.Length)
            character = characters.ToCharArray()(index).ToString()
        Loop While id.IndexOf(character) <> -1
        id += character
    Next

    Return id
End Function