Randomly generated random numbers and non-repeating random digits in Java

Source: Internet
Author: User

Generate random numbers in Java


1. Call Java.lang the random () method in the math class below to produce an arbitrary number

public class Myrandom {
public static void Main (string[] args) {

int radom = (int) (Math.random () *10);

System.out.println (Radom);

}
}


where Math.random ()//produces a random decimal between 0~1.

Produces an integer between 0~9: (int) (Math.random () *10);

Produces an integer between 1~10 that can be written: (int) (Math.random () *10 + 1);

And so on: produce a number between 0~n should write: Math.random () *n;

Take out a random positive integer of the specified length size:

public static int Buildrandom (int length) {
int num = 1;
Double random = Math.random ();
if (Random < 0.1) {
Random = random + 0.1;
} for (int i = 0; i < length; i++) {
num = num * 10;
}
return (int) ((random * num));
}
2. Call Java.util The following random class, where an instance of this class is used to generate a pseudo-random number stream, producing a random integer, then calling the class's Nextint () method

Before using the Random class, the Java.util.Random is imported under the package, and the code is:

Import Java.util.Random;
public class Myrandom {
public static void Main (string[] args) {
Random rand = new Random ();
int rInt = Rand.nextint (10);
System.out.println (RINT);

}
}


where random rand = new Random () is the creation of a new random number generator; rand.nextint (int n) is an int value that is taken out of the sequence of this random number generator, evenly distributed between 0 (inclusive) and the specified value n (not included).

In Java in a specified integer range class, the loop produces a different random number

The following is an example of an integer that produces a difference of 6 bits 20:

public class Myrandom {
public static void Main (string[] args) {
int n = 20;
Random rand = new Random ();
boolean[] bool = new Boolean[n];
int randint = 0;

for (int i = 0; i < 6; i++) {
do {
Randint = Rand.nextint (n);

}while (Bool[randint]);
Bool[randint] = true;
System.out.println (Randint);

}
}


}
It uses a Boolean array of variables to store whether the number is generated. After it is generated, the number becomes true as the corresponding Boolean value for the following table in the Boolean array, and the next time the number is generated, it enters the do...while loop again to generate the number until the number that has not been generated is produced.

For example, an array of type int is generated with a length of 50 and a number between 0-50 is inserted, and cannot be duplicated.

The code is as follows:
public class Myrandom {
public static void Main (string[] args) {
int[] Intrandom = new INT[50];
List mylist = new ArrayList (); Generates a dataset to hold the resulting number and to determine
Random rd = new random ();
while (Mylist.size () < 50) {
int num = Rd.nextint (51);
/** uses the Contains method of the collection to determine whether the data set contains the number of NUM,
* If contains return true. Does not contain is false.! indicates "non".
*!mylist.contains (num)) is a Boolean value that performs its internal operation only if the value is true, which is not included.
*/
if (!mylist.contains (num)) {
Mylist.add (num); Add data to the collection.
}
}
/** assigning values to a value */
for (int i = 0;i <mylist.size (); i++) {
Intrandom[i] = (Integer) (Mylist.get (i));
}
}
}

Randomly generated random numbers and non-repeating random digits in Java

Related Article

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.