Generates a random number of 90-100 repetitions:
public class RandomTest { public static void main(String[] args){ /* * The Math.random() method defaults to double type, so it needs to be cast to int */ int x=(int)(Math.random()*(100-90+1)+90); //(max-min+1)+min=min-max System.out.println(x); } }Generates 90-100 non-repetition random numbers:
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 value int min=90; //minimum value int count=max-min; //random number Random random = new Random();Set<Integer> set=new HashSet<>(); //hashset container can only store non-duplicate objects while(set.size()<count){ //number of elements stored inhashset int x = random.nextInt(max-min+1)+min; //Create a random number set.add(x); //Add a random number into the hashset container}for(int i:set){ //Foreach traversal the container element System.out.println(i);}}}}A random number of 90-100 repetitions is generated per second:
import java.util.Random;import java.util.Timer;import java.util.TimerTask;public class RandomTest { void timer(){Timer timer = new Timer(); //Create the timer.schedule(new TimerTask() {public void run() { //TimerTask run method to implement Runnable interface Random random = new Random(); int x = random.nextInt(100-90+1)+90; //(max-min+1)+min=min to max// int x=random.nextInt(100)%(100-90+1) + 90; //Same effect System.out.println(x);}},0,1000); //0 means no delay, 1000ms=1s} public static void main(String[] args){ RandomTest ran=new RandomTest(); ran.timer(); //Call the timed task} }This article is reproduced at: https://www.idaobin.com/archives/301.html