Gets 10 random numbers between 1-20 and requires no repetition

Source: Internet
Author: User
Tags repetition

Requirement: Get 10 random numbers between 1-20 and require no repetition

/**
Analysis
*A: Create an object that produces a random number
*B: Create a collection that stores random numbers.
*C: Define a statistical variable. Starting from 0.
*D: Determine if the statistical traversal is less than 10
*is: A random number is generated to determine whether the random number exists in the collection.
*If it does not exist: Add, Statistics variable + +.
*If there is: do not respond to it.
*No: Ignore it.
*E: Iterating through the collection
*/


<span style= "FONT-SIZE:18PX;" >public class Settest {public static void main (string[] args) {Random random = new random (); int count = 0; list<integer>  list = new arraylist<integer> (), while (count<10) {int randomnumber = Random.nextint ( +1;if (!list.contains (Randomnumber)) {List.add (randomnumber); count++;}} for (int i=0;i<list.size (); i++) {System.out.println (List.get (i));}}} </span>

This is the use of the list collection, if you have a set set of features that cannot be repeated, then I do not need to add to the collection of data to judge

<span style= "FONT-SIZE:18PX;" >public class Settest {public static void main (string[] args) {Random random = new random (); hashset<integer>  set = new Hashset<integer> (), while (Set.size () <10) {int randomnumber = Random.nextint (+1;set.add) (Randomnumber);} for (Integer i:set) {System.out.println (i);}}} </span>


Gets 10 random numbers between 1-20 and requires no repetition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.