Click here to Skip to main content
15,917,859 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i am selecting question randomly from database,same question is repeated more than once.how can I solve this?
Posted
Comments
Abhinav S 27-May-10 3:11am    
Reason for my vote of 1
What have you tried so far? What you have asked is homework to me.
R. Giskard Reventlov 27-May-10 3:28am    
Sounds like it's not quite as random as you think it is. How do you evaluate that the selection is, indeed, random?

BTW: do you understand that writing a question in txtspk is rude and makes you look like a child trying to get their homework done. A big no-no round here.

This is a bit of a guess, without seeing your code it is difficult to tell what you are doing wrong. However, the most common error when using random numbers is to re-create the randomiser repeatedly:
for (int i = 0; i < 10; i++)
   {
   Random r = new Random();
   outputMyRandomNumbers[i] = r.Next();
   }
This will generate the same number several times in a row, possibly every time depending on when it happens, processor speed, etc.

Change it to a single instance:
Random r = new Random();
for (int i = 0; i < 10; i++)
   {
   outputMyRandomNumbers[i] = r.Next();
   }
And all will be well.
 
Share this answer
 
Receiving the following output from a random number generator is perfectly valid, if not very likely:

1, 1, 1, 1, 2, 1, 1, 1

It is, after all, a set of random numbers. However, to get around your problem, why not maintain a list of questions not asked, and then pick a random index from that list. Then it's impossible to get a repeated question, even if the random number is the same every time (provided you remember to remove the question from the list once asked).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900