Ramdom number without repetation

Posted by Srinu0411 under ASP.NET on 2/25/2010 | Views : 1685 | Status : [Member] | Replies : 5
hi

i want to pick questions ramdomly from set of questions,i tied in this way but some questions r repeating

Random rnd = new Random();
int i = rnd.Next(0, x);

please tell any budy ramdom number with out repetation

kaja srinivas


Responses

Posted by: Abhi2434 on: 2/25/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Use a list of all random number already generated.

Say

List<int> randquestionalreadyserved = new List();

Now for every random number, put it in the list and check the list before you decide. So your code will look like :

Random md = new Random();

while(true){
int rnd = md.Next(0,x);
if(!randquestionalreadyserved.Contains(rnd))
{
randquestionalreadyserved.Add(rnd);
break;
}
}


I hope this would help you. Cheers



www.abhisheksur.com

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

Posted by: Nishithraj on: 2/25/2010 [Member] Bronze

Up
0
Down
Check the threads in the following link. It will clear your doubts

http://www.codeproject.com/answers/59032/How-to-avoid-duplicate-random-questions.aspx

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

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

Posted by: Srinu0411 on: 2/25/2010 [Member] Starter

Up
0
Down
Thanks

kaja srinivas

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

Posted by: Abhi2434 on: 2/27/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
If it really helped u, please mark it as Resolved.

www.abhisheksur.com

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

Posted by: Srinu0411 on: 2/28/2010 [Member] Starter

Up
0
Down
Thanks i got ramdom number with out repetation

kaja srinivas

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

Login to post response