Select random record from String Array using C# and VB.Net

Last Reply 13 days ago By dharmendr

Posted 14 days ago

Hello,

how can i select random name?

i'll try this but it's can't

To give an example: car, house, bus I want to put them on the label as 1 random record

private void RandName()
{
    string[] maleNames = new string[1000] { "aaron", "abdul", "abe", "abel", "abraham", "adam", "adan", "adolfo", "adolph", "adrian"};
    string[] femaleNames = new string[1000] { "abby", "abigail", "adele", "adrian"};
    string[] lastNames = new string[1000] { "abbott", "acosta", "adams", "adkins", "aguilar"};
    Random rand = new Random(DateTime.Now.Second);
    if (rand.Next(1, 2) == 1)
    {
        FirstName = maleNames[rand.Next(0, maleNames.Length - 1)];
    }
    else
    {
        FirstName = femaleNames[rand.Next(0, femaleNames.Length - 1)];
    }
}

 

You are viewing reply posted by: dharmendr 13 days ago.
Posted 13 days ago

Hi merictokatlio...,

Refer below code.

C#

protected void Page_Load(object sender, EventArgs e)
{
    string[] names = new string[] { "aaron", "abdul", "abe", "abel", "abraham", "adam", "adan", "adolfo", "adolph", "adrian" };
    int index = new Random().Next(names.Length);
    Response.Write(names[index]);
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim names As String() = New String() {"aaron", "abdul", "abe", "abel", "abraham", "adam", "adan", "adolfo", "adolph", "adrian"}
    Dim index As Integer = New Random().Next(names.Length)
    Response.Write(names(index))
End Sub

Screenshot