# how to get random numbers from given array

Posted by Writetopallavi under C# on 12/14/2011 | Points: 10 | Views : 4173 | Status : [Member] | Replies : 5
Hi all
I want to find random numbers from an given array for that i use following logic but i am not getting expected answer yet
protected void Page_Load(object sender, EventArgs e)
{
int[] numarr = { 14, 22, 44, 33, 55, 82, 91, 16, 17, 15 };
int[] a = new int[10];
int[] b = new int[10];
int no = 0;
int count = numarr .Length;
Random RandomNumber = new Random();
for (int i = 0; i < count; i++)
{
no = RandomNumber.Next(1, count);
if (!b.Contains(no))
{
b[i] = no;
a[no] = numarr [i];
}
}

foreach (int j in a)
{
Response.Write("First Number:" + j + "</br>");
}
}

In this code the problem is RandomNumber.Next gives duplicate numbers
I just wants to change the sequence of numbers in numarr array

#### Responses

Posted by: Hmanjarawala on: 12/14/2011 [Member] Bronze | Points: 25
 0 for this your logic should be: ```protected void Page_Load(object sender, EventArgs e) { int[] numarr = { 14, 22, 44, 33, 55, 82, 91, 16, 17, 15 }; int[] a = new int[10]; int[] b = new int[10]; int no = 0; int count = numarr .Length; Random RandomNumber = new Random(); for (int icnt = 0; icnt < count; i++) { repeat: no = RandomNumber.Next(1, count); if (!b.Contains(no)) { b[icnt] = no; a[no] = numarr[icnt] ; } else goto reteat; } foreach (int j in a) { Response.Write("First Number:" + j + "
 0 Hi, if your issue is just to change the sequence then have this code. it may helps you a lot: ```for (int icnt = 0; icnt < count; i++) { no = RandomNumber.Next(1, count); numarr[icnt] = numarr[icnt] + numarr[no-1]; numarr[no-1] = numarr[icnt] - numarr[no-1]; numarr[icnt] = numarr[icnt] - numarr[no-1]; } ``` replace thos for loop with your existing loop. and keep this code as it is.Himanshu Manjarawala Sr. Software Engineer@AutomationAnywhere http://fieredotnet.wordpress.com/Writetopallavi, if this helps please login to Mark As Answer. | Alert Moderator