In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to realize the lucky draw algorithm in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to realize the lucky draw algorithm by Java".
Description of the topic
Topic: in order to give benefits to fans, Xiao Xuzhu decided to pick a lucky fan among the fans who participated in learning to sign in and give them a small gift. In order to be fair, make sure that the lottery process is random.
Second, the way to solve the problem
1. Add the participants to the collection
2. Use Random object to get random numbers
3. Use the random number as the subscript to get the lucky users in the set
3. Public class Basics28 {public static void main (String [] args) {List luckUserNames = getLuckUserNames (); Random random = new Random (); int luckNum = random.nextInt (6); System.out.println ("the lucky future boss is: + luckUserNames.get (luckNum));} private static List getLuckUserNames () {List luckUserNames = new ArrayList (); luckUserNames.add (" Li slowly _ ") LuckUserNames.add ("TryAgain-"); luckUserNames.add ("team_dog"); luckUserNames.add ("Jasonakeke"); luckUserNames.add ("Xiao Wang who is good at learning c language"); luckUserNames.add ("Ara~ running after the wind"); return luckUserNames;}}
Fourth, optimize the lottery algorithm to solve the problem.
The random number is too small and needs to be optimized.
It is necessary to list the number of random hits by everyone and show it, which is more open and fair.
The code details public class LuckDraw {public static void main (String [] args) {List luckUserNames = getLuckUserNames (); Random random = new Random (); / / key:luckNum, value:count Map luckNumMap = new HashMap (); for (int I = 0; I < 100; iTunes +) {int luckNum = random.nextInt (4); Integer count = luckNumMap.get (luckNum) If (count==null) {count= 0;} + count; luckNumMap.put (luckNum,count);} Integer maxLuckNum = 0; Integer maxCount = 0; for (Integer luckNum:luckNumMap.keySet ()) {if (maxCount)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.