A method for generating random numbers in Java _java

Source: Internet
Author: User
Tags array length repetition

The example in this article describes the method by which Java generates random numbers. Share to everyone for your reference. The implementation method is as follows:

Package Com.tool.code; 
Import Java.util.Random; public class Randomnum {private static char ch[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ' , ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' X ', '
 Y ', ' z ', ' 0 ', ' 1 '}; 
Finally, repeat two 0 and 1, because you need to fill the array length for the Random private static Random = new Random (); 
  Generates a random string of specified lengths public static string createrandomstring (int length) {if (length > 0) {int index = 0; 
  char[] temp = new Char[length]; 
  int num = Random.nextint ();
   for (int i = 0; i < length% 5; i++) {temp[index++] = ch[num & 63]; 
   Take the back six digits, remember that the corresponding binary is in the form of complement. Num >>= 6;//63 binary is: 111111//Why should I move 6 digits to the right?
   Because there are 64 valid characters in the array. Why do I have to save 5?
  Because an int type is represented by 4 bytes, that is, 32 bits. 
   for (int i = 0; i < LENGTH/5 i++) {num = Random.nextint (); For(int j = 0; J < 5; J + +) 
    {temp[index++] = ch[num & 63]; 
   Num >>= 6; 
 Return the new String (temp, 0, length); 
 else if (length = = 0) {return ""; 
 else {throw new illegalargumentexception (); 
 The repeat rate of the random string function is the public static double rateofrepeat (int number) {int repeat=0), based on the specified number; 
 String[] Str=new String[number]; 
 for (int i=0;i<number;i++) {//Generate a specified number of string str[i]=randomnum.createrandomstring (10); for (int i=0;i<number;i++) {///find if there is the same string for (int j=i+1;j<number-1;j++) {if (Str[i].equals (STR[J)) rep 
  eat++; 
} return (double) repeat)/number; public static void Main (string[] args) {System.out.println (randomnum.createrandomstring (16) + "-" + 
 Randomnum.createrandomstring (16)); 
Double Rate=randomnum.rateofrepeat (10000),//test 10,000 times repetition rate System.out.println ("Repetition rate: +rate"); }
}

The effect of the console operation is as follows:

I hope this article will help you with your Java programming.

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.