Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my database i had stored 500 records its primary key is ID(number)
Now i want to show the record for user randomly without repetition.As per my requirement each user i will show one id randomly generaed without repetition..
Posted
Updated 19-May-15 19:29pm
v2
Comments
virusstorm 19-May-15 15:43pm    
What do you mean by repetition?
venkatesh@India 19-May-15 15:54pm    
Same number
ZurdoDev 19-May-15 15:44pm    
Where are you stuck? If you want to show random but without repetition you have to store which ones you did and then generate a new random number and if you have done that one, generate another random number until you have one you haven't done.

Make an array of the IDs and then shuffle the array.
Look at my solution in: playing cards game (make Random 4 groups of Numbers in Main Group)[^] for shuffling.
Present the records in the shuffled order.
Don't re-use a record, once presented.
Shuffle again for the next sequence of records.

[Edit: MTH]
First get an array of the record objects (or just the IDs). Call it orderedData.
Call var shuffledData = Shuffle(orderedData); (see below)
Use the values of shuffledData[0 thru end] in sequence and it will be the record objects (or IDs, as above) in a random order with no repeats.

Code for Shuffle() converted to generic:
C#
private Random rand = new Random();
public static T[] Shuffle<T>(T[] data)
{
  T[] shuffled = new T[data.Length];
  shuffled[0] = data[0];
  for (int i = 1; i < data.Length; i++)
  {
    int j = rand.Next(i + 1);
    if (j != i)
      shuffled[i] = shuffled[j];
    shuffled[j] = data[i];
  }
  return shuffled;
}

[Edit #2: MTH]: I renamed the variable outside of the Shuffle method so there's no confusion about names.
 
Share this answer
 
v3
Comments
venkatesh@India 19-May-15 16:23pm    
Can you explain clearly;
Matt T Heffron 19-May-15 16:45pm    
See updated solution.
venkatesh@India 20-May-15 1:26am    
@Matt T Heffron .The data means ID[1-500]
Matt T Heffron 20-May-15 12:59pm    
If you are guaranteed that those are the only possible values for the IDs, then you can construct data like:
int[] data = Enumerable.Range(1, 500).ToArray();
If it is possible at a later date that the database will have fewer or additional IDs, or that they aren't exactly sequential, then extracting them from your database would be a better design.
(Note the edit I made to the solution above. This data is what I renamed to be orderedData in the solution. This assignment is outside of the Shuffle() method.)
Sure, and its quite easy.

SQL
SELECT TOP 1 * FROM table ORDER BY NEWID()


Wish I could take credit, but the answer comes from Stack Overflow[^]

Hogan
 
Share this answer
 
Comments
venkatesh@India 19-May-15 16:00pm    
@snorkie each and every times shows different ID .
Matt T Heffron 19-May-15 16:04pm    
Doesn't guarantee no repeats :-(
 
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