The example usage of Java Random number and timer
This article mainly introduces "the example usage of Java random number and timer". In the daily operation, I believe that many people have doubts about the example usage of Java random number and timer. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the example usage of Java random number and timer". Next, please follow the editor to study!
Produce a repeated random number of 90-100:
Public class RandomTest {public static void main (String [] args) {/ * * Math.random () method defaults to double type, so it needs to be cast to int * / int x = (int) (Math.random () * (100-90) + 90); / / (max-min+1) + min=min-max System.out.println (x);}}
Generate 90-100 random numbers that are not repeated:
Import java.util.HashSet;import java.util.Random;import java.util.Set;public class RandomTest {public static void main (String args []) {int max=100; / / maximum int min=90; / / minimum int count=max-min; / / Random number Random random = new Random (); Set set=new HashSet () / / only non-duplicated objects while (set.size ()) can be stored in the hashset container.