The description of the basic algorithm is as follows:
1. Increase the weight of each advertisement
2. Add the weight of all matching advertisements to SUM,
3. Seeds with the additional result as a random number, generate a random number RD between 1 ~ SUM
4. Then traverse all the advertisements, the access order can be freely. Put the weight of the current node with the weight of each node visited earlier, which is worth CURWT, judge Curwt> RD, if the conditions are established, the current node will be returned. A node. Until the above conditions, due to RD <= Sum, there must be Curwt> = RD.
Special description:
The order of this algorithm has nothing to do with the order of advertising
Import java.util.arrayList; Import Java.util.Collections; Import java.util.comparators; Import java.util.linkedhashmap; Import port java.util.map; Public class test { /*** @Param ARGS */ @SuppressWarnings ("Uncheck") Public Static Void Main (String [] Args) {list <node> Arrnodes = New ArrayList <node> (); w node (10, "Test 1") ; Arrnodes.add (n); n = New Node (20, "Test 2"); Arrnodes.add (N); n = New Node (30, "Test 3"); Arrnodes.add (n); n = New Node (40, "Test 4"); Arrnodes.add (n); //Collections.sort (arrnodes, New Node ()); MAP <string, Integer> Showmap = NULL; Int Sum = Getsum ES); int Random = 0; node kw = null; for (int k = 0; k <20; k ++) {showmap = new linkedhashmap <string, integer> (); for (int i = 0; i <100; i ++) { random = getrandom (SUM); kw = getkw (Arrnodes, RANDOM); if (showmap.containskey (kw.kw)) {showmap.put (kw.kw, showmap.get (kw.kw) + 1); else {showmap.put (kw.kw, 1);} //system.out.println (i + "" + random + "" + getkw (Arrnodes, RANDOM));} System.out.print (k + "" "" " );;:}}} Public static node getkw (list <node> nodes, int RD) {node Ret = null; int Curwt = 0; for (n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: n: c urwt += n .weight; if (Curwt> = RD) {Ret = n; Break;} Return Ret;} Public Static Int Getsum (List <node> nodes) {int Sum = 0; += n.weight; Return Sum;} Public Static Int Getrandom (int Seed) {Return (int) math.round (math.random () * seed);} Class Node Implements int weight = 0; string kw = " "; Public Node () {} Public Node (int WT, String Kw) {this.weight = wt; this.kw = kw;} Public String Tostring () {StringBuilder Sbbuilder = NE w strongbuilder (); sbbuilder.append (" Weight = "). Append (weight); sbbuilder.append (" kw "). Append (kw); Return sbbuilder.tostring ();} Public int compare (Object O1, Object O2) de n1 = (node) o1 ; Node n2 = (node) O2; if (n1.weight> n2.weight) Return 1; else return 0;}}