史上最全的隨機數產生java演算法

來源:互聯網
上載者:User

標籤:java   隨機演算法   java工具   

原文:史上最全的隨機數產生java演算法

原始碼:http://www.zuidaima.com/share/1585762703215616.htm

前段時間有其他牛人分享的隨機數激起了我分享隨機數的想法

java隨機字元補充版

【maven+junit】 java產生指定為位元的隨機密碼

我分享一個最全的隨機數的產生演算法,最代碼的找回密碼的隨機數就是用的這個方法:

1 String password = RandomUtil.generateString(10);

源碼如下:

package com.javaniu.core.util;import java.util.Random;/*** @author www.zuidaima.com**/public class RandomUtil {public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";public static final String NUMBERCHAR = "0123456789";/** * 返回一個定長的隨機字串(只包含大小寫字母、數字) *  * @param length *            隨機字串長度 * @return 隨機字串 */public static String generateString(int length) {StringBuffer sb = new StringBuffer();Random random = new Random();for (int i = 0; i < length; i++) {sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));}return sb.toString();}/** * 返回一個定長的隨機純字母字串(只包含大小寫字母) *  * @param length *            隨機字串長度 * @return 隨機字串 */public static String generateMixString(int length) {StringBuffer sb = new StringBuffer();Random random = new Random();for (int i = 0; i < length; i++) {sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));}return sb.toString();}/** * 返回一個定長的隨機純大寫字母字串(只包含大小寫字母) *  * @param length *            隨機字串長度 * @return 隨機字串 */public static String generateLowerString(int length) {return generateMixString(length).toLowerCase();}/** * 返回一個定長的隨機純小寫字母字串(只包含大小寫字母) *  * @param length *            隨機字串長度 * @return 隨機字串 */public static String generateUpperString(int length) {return generateMixString(length).toUpperCase();}/** * 產生一個定長的純0字串 *  * @param length *            字串長度 * @return 純0字串 */public static String generateZeroString(int length) {StringBuffer sb = new StringBuffer();for (int i = 0; i < length; i++) {sb.append('0');}return sb.toString();}/** * 根據數字產生一個定長的字串,長度不夠前面補0 *  * @param num *            數字 * @param fixdlenth *            字串長度 * @return 定長的字串 */public static String toFixdLengthString(long num, int fixdlenth) {StringBuffer sb = new StringBuffer();String strNum = String.valueOf(num);if (fixdlenth - strNum.length() >= 0) {sb.append(generateZeroString(fixdlenth - strNum.length()));} else {throw new RuntimeException("將數字" + num + "轉化為長度為" + fixdlenth+ "的字串發生異常!");}sb.append(strNum);return sb.toString();}/** * 每次產生的len位元都不相同 *  * @param param * @return 定長的數字 */public static int getNotSimple(int[] param, int len) {Random rand = new Random();for (int i = param.length; i > 1; i--) {int index = rand.nextInt(i);int tmp = param[index];param[index] = param[i - 1];param[i - 1] = tmp;}int result = 0;for (int i = 0; i < len; i++) {result = result * 10 + param[i];}return result;}public static void main(String[] args) {System.out.println("返回一個定長的隨機字串(只包含大小寫字母、數字):" + generateString(10));System.out.println("返回一個定長的隨機純字母字串(只包含大小寫字母):" + generateMixString(10));System.out.println("返回一個定長的隨機純大寫字母字串(只包含大小寫字母):"+ generateLowerString(10));System.out.println("返回一個定長的隨機純小寫字母字串(只包含大小寫字母):"+ generateUpperString(10));System.out.println("產生一個定長的純0字串:" + generateZeroString(10));System.out.println("根據數字產生一個定長的字串,長度不夠前面補0:"+ toFixdLengthString(123, 10));int[] in = { 1, 2, 3, 4, 5, 6, 7 };System.out.println("每次產生的len位元都不相同:" + getNotSimple(in, 3));}}


有圖有真相:


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

史上最全的隨機數產生java演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.