博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
随机数生成器
阅读量:6803 次
发布时间:2019-06-26

本文共 2903 字,大约阅读时间需要 9 分钟。

一个随机数生成器

package com.dy.xidian;import java.net.*;import java.util.*;import java.security.*;class RandomGUID extends Object {    public String valueBeforeMD5 = "";    public String valueAfterMD5 = "";    private static Random myRand;    private static SecureRandom mySecureRand;    private static String s_id;    static {        mySecureRand = new SecureRandom();        long secureInitializer = mySecureRand.nextLong();        myRand = new Random(secureInitializer);        try {            s_id = InetAddress.getLocalHost().toString();        } catch (UnknownHostException e) {            e.printStackTrace();        }    }    public RandomGUID() {        getRandomGUID(false);    }    public RandomGUID(boolean secure) {        getRandomGUID(secure);    }    private void getRandomGUID(boolean secure) {        MessageDigest md5 = null;        StringBuffer sbValueBeforeMD5 = new StringBuffer();        try {            md5 = MessageDigest.getInstance("MD5");        } catch (NoSuchAlgorithmException e) {            System.out.println("Error: " + e);        }        try {            long time = System.currentTimeMillis();            long rand = 0;            if (secure) {                rand = mySecureRand.nextLong();            } else {                rand = myRand.nextLong();            }            sbValueBeforeMD5.append(s_id);            sbValueBeforeMD5.append(":");            sbValueBeforeMD5.append(Long.toString(time));            sbValueBeforeMD5.append(":");            sbValueBeforeMD5.append(Long.toString(rand));            valueBeforeMD5 = sbValueBeforeMD5.toString();            md5.update(valueBeforeMD5.getBytes());            byte[] array = md5.digest();            StringBuffer sb = new StringBuffer();            for (int j = 0; j < array.length; ++j) {                int b = array[j] & 0xFF;                if (b < 0x10) sb.append('0');                sb.append(Integer.toHexString(b));            }            valueAfterMD5 = sb.toString();        } catch (Exception e) {            System.out.println("Error:" + e);        }    }    public String toString() {        String raw = valueAfterMD5.toUpperCase();        StringBuffer sb = new StringBuffer();        sb.append(raw.substring(0, 8));        sb.append("-");        sb.append(raw.substring(8, 12));        sb.append("-");        sb.append(raw.substring(12, 16));        sb.append("-");        sb.append(raw.substring(16, 20));        sb.append("-");        sb.append(raw.substring(20));        return sb.toString();    }    public static void main(String args[]) {        for (int i=0; i< 5; i++) {        RandomGUID myGUID = new RandomGUID();        System.out.println("Seeding String=" + myGUID.valueBeforeMD5);        System.out.println("rawGUID=" + myGUID.valueAfterMD5);        System.out.println("RandomGUID=" + myGUID.toString());        }    }}

执行后结果:

 

转载于:https://www.cnblogs.com/xidongyu/p/5300639.html

你可能感兴趣的文章
oracle内存体系(二)
查看>>
ReflectASM的使用
查看>>
智能家居监控移动手机组态现实生活中的应用
查看>>
笔试题、面试题
查看>>
shell 数组
查看>>
Linux操作系统的安装
查看>>
服务器RAID
查看>>
python函数是引用传递(对可变对象而言)
查看>>
Cisco ASA防火墙简易配置与调试手册
查看>>
Node.js搭建聊天室
查看>>
Rob Pike的5个编程原则
查看>>
Expression Blend实例中文教程(7) - 动画基础快速入门Animation
查看>>
《第一行代码》1day~了解全貌
查看>>
复习javaIO 之File类
查看>>
How to install snmpwalk snmpget on CentOS 6.4 6.3 5.9 Redhat RHEL Fedora
查看>>
最小生成树
查看>>
Mybatis中配置Mapper的方法
查看>>
Java基础学习总结(19)——Java环境变量配置
查看>>
Mvc5+Entity Framework6 之二----在MVC中用Entity Framework实现基本的CRUD
查看>>
我的友情链接
查看>>