How to use Java code to generate several random numbers of two-color ball
This article introduces the knowledge of "how to use Java code to generate several random numbers of two-color balls". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Mavan pom file reference dependency
Cn.hutool hutool-all 5.3.6 com.google.guava guava 18.0
Java single-class implementation code, in the editor, paste the following code. Right mouse button, run running
Import cn.hutool.core.util.RandomUtil;import com.google.common.collect.Lists; import java.util.List; / * * @ Author tarzan * @ Date 14:43 on 2021-4-1 * @ Description simulates the two-color ball and randomly generates several residential numbers * / public class DoubleColorBall {/ * main method * * / public static void main (String [] args) {getDoubleColorBallNumber (5) } / * get multi-note two-color ball number * * / public static void getDoubleColorBallNumber (int num) {System.out.println ("randomly generated" + num+ "note two-color ball number is:"); String resultNumber= "; for (int I = 0; I < num; iTunes +) {System.out.println (" ["+ (iTun1) +"] "+ resultNumber+getDoubleColorBallNumber () }} / * get the single-note two-color ball number * * / public static String getDoubleColorBallNumber () {String resultNumber= "; for (int I = 0; I < 6; iTunes +) {String ballNumber= RandomUtil.randomEle (getRedBalls ()) +"; resultNumber=resultNumber+ballNumber;} return resultNumber+RandomUtil.randomEle (getBlueBalls ()) } / * get the collection of red ball numbers * * / public static List getRedBalls () {return getBalls (33);} / * get the collection of blue ball numbers * * / public static List getBlueBalls () {return getBalls (16);} / * get the collection of ball numbers * * / public static List getBalls (int num) {List redBalls= Lists.newArrayList () For (int I = 1; I