When you are playing Weibo every day, there is a "person you may know" on the Weibo broadcast content. Every time you refresh the page, the data there will change randomly, I will share with you today that I will write a general description of the random display of data called from the database.Algorithm, Mainly mark. Haha...
Paste directlyCode.
Random random = New Random (); datatable dt = New Datatable (); stringbuilder sb = New Stringbuilder (); // Int COUNT = DT. Rows. count; Int Count = 20 ; // Maximum number of index records Int Times = 2 ; // Record cycles If (Count <= 6 ) // Suppose I want to retrieve 6 data records. { For ( Int I =0 ; I <count; I ++ ) {Sb. append ( "" + I. tostring () + " , " ); // String to be spliced }} Else { String Strindex = "" ; // Used to compare non-repeated strings Int N = 0 ; // Cyclic count Int Islast = 0 ; // Last index value For ( Int I = 0 ; I <times; I ++) // How to control the number of cycles { Int Index = random. Next ( 0 , Count ); If (I = 0 ) {N ++ ; Strindex + = Index. tostring () + " , " ; Sb. append (index. tostring () + " , " ); // String to be spliced Islast = Index ;} Else { If (Islast! = Index ){ String [] Strlast = strindex. Split ( ' , ' ); Int M = 0 ; For ( Int J = 0 ; J <strlast. Length- 1 ; J ++) // Prevent duplication { If (Convert. toint16 (strlast [J]) =Index) {m ++ ; Break ;}} If (M = 0 ) {N ++ ; Strindex + = Index. tostring () + " , " ; Sb. append (index. tostring () + " , " ); // String to be spliced Islast = Index ;}}} If (N = 5 ){ Break ;} Times ++ ;}} // View results Clientscript. registerstartupscript ( Typeof (Page ), "" , " Alert (' " + Sb. tostring () + " Times: " + Times + " ') " ,True );
PS: Tandy tang I wish you a pleasant time writing your code!