how to get random numbers from given array

Posted by Writetopallavi under C# on 12/14/2011 | Points: 10 | Views : 3986 | 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
Please help...




Responses

Posted by: Hmanjarawala on: 12/14/2011 [Member] Bronze | Points: 25

Up
0
Down
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 + "</br>");
}
}


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Writetopallavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Writetopallavi on: 12/16/2011 [Member] Starter | Points: 25

Up
0
Down
Hello sir,

Thanks for your immediate reply.
I executed this code but for loop is continuously looping
Actually my issue is i want to change the sequence of numbers in given array.




Writetopallavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 12/16/2011 [Member] Bronze | Points: 25

Up
0
Down
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

Posted by: Writetopallavi on: 12/16/2011 [Member] Starter | Points: 25

Up
0
Down
Hi
my problem get solved , I am so happy..
I used following code
int[] numarr = { 14, 22, 44, 33, 55, 82, 91, 16, 17, 15 };
int count = numarr.Length;
int[] a = new int[count];
int[] b = new int[count];
int no = 0;
Random RandomNumber = new Random();

for (int icnt = 0; icnt < count; icnt++)
{
repeat:
no = RandomNumber.Next(1, count);
if (!b.Contains(no))
{
b[icnt] = no;

a[no] = numarr[icnt];

}
else
{
if (b[count-2] != 0)
{
a[0] = numarr[icnt];
break;
}
goto repeat;
}
}
foreach (int j in a)
{

Response.Write("First Number:" + j+ "</br>");

}
}

Writetopallavi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 12/16/2011 [Member] Bronze | Points: 25

Up
0
Down
Ok,

thats gr8.

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Writetopallavi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response