Get the App
SLTechnology News&Howtos  ›  Development  › 

The example usage of Java Random number and timer

Shulou Source: shulou.com Published: 2022-06-03 19:40:22 09月26日 Update

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.

Tags: Random number timer instance learning container method element object more help practical maximum minimum next number task interface effect number article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei OPPO Reno Shulou Tech Info Redmi