How to generate random numbers in Java

Source: Internet
Author: User
Tags array length

This article illustrates the method of generating random numbers in Java. Share to everyone for your reference. The implementation methods are 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 back six digits, remember that the corresponding binary system 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[nu M & 63]; Num >>= 6; Return the new String (temp, 0, length); else if (length = = 0) {return "";} else {throw new IllegalArgumentException ();}} Tests the repeat rate of a random string function, based on the number of specified numbers, the public static double rateofrepeat (int number) {int repeat=0; String[] Str=new String[number]; for (int i=0;i<number;i++) {//Generate a specified number of strings str[i]=randomnum.createrandomstring;} 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))) repeat++;} 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 repeat rate System.out.println ("repeat rate: +rate);}"

The effect of the console operation is as follows:

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

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.