Asp.net random display of retrieved data (original)

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.