Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate multiple random numbers at a time by java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces java how to generate multiple random numbers at a time related knowledge, detailed and easy to understand, simple and fast operation, with certain reference value, I believe everyone read this java how to generate multiple random numbers at a time article will have some gains, let's take a look at it.

This tutorial operating environment: Windows 7 system, Java10 version, DELL G3 computer.

Create a list collection, Random object. Write a while loop that puts randomly generated random numbers into the set (before putting in, determine whether the generated random number exists in the set, if it exists, discard it, if it does not exist, put it in the set)

import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Random; public class Test { public static void main(String[] args) { //Call the function. The requirement is to generate 10 random numbers in the range of [0,100]. System.out.println(getRandomNumList(10,0,100)); } //Defines how to generate random numbers and load them into collection containers //The parameter list of the method is: the number of generated random numbers, the range of generated random numbers, the minimum value is start(including start), and the value range is end(excluding end). The range of values can be expressed as [start,end]. public static List getRandomNumList(int nums,int start,int end){ //1. Create a collection container object List list = new ArrayList(); //2. Create Random object Random r = new Random(); //loop judge the random number obtained. If the random number does not exist in the set, put the random number into the set. If it exists, discard the random number without doing operation, and perform the next loop until the set length is equal to nums. while(list.size() != nums){ int num = r.nextInt(end-start) + start; if(! list.contains(num)){ list.add(num); } } return list; }}

Math.radio will randomly generate numbers greater than or equal to 0.0 and less than 1.0. Note that 1 cannot be generated here.

public class a { public static void main(String[] args) {int min=10,max=100;for(int i=0;i

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report