SecureRandom secureRandom1 = SecureRandom.getInstance("SHA1PRNG")
SecureRandom secureRandom2 = SecureRandom.getInstance("SHA1PRNG")
System.out.println(secureRandom2.equals(secureRandom1))
byte[] seed={12,11,123}
secureRandom1.setSeed(seed)
secureRandom2.setSeed(seed)
System.out.println(secureRandom1.nextInt())
System.out.println(secureRandom2.nextInt())
}
每次都新生成一个SecureRandom 对象,而SecureRandom 没有覆盖equals方法
所以它用超类Ojbect的equals方法
public boolean equals(Object obj) {
return (this == obj)
}
所以 System.out.println(secureRandom2.equals(secureRandom1))
的结果是false
但是两个对象的内部结构是一样的,所以它们设置相同的seed,执行相同方法的结果是一样的。可以查看jdk源码
1.打开任意网页如 www.baidu.com
2.添加个人书签:
3.修改名称
4.编辑地址
5将下面的有道翻译脚本复制到地址中
javascript: void((function() {var element = document.createElement('script')element.id = 'outfox_seed_js'element.charset = 'utf-8',element.setAttribute('src', 'http://fanyi.youdao.com/web2/seed.js?' + Date.parse(new Date()))document.body.appendChild(element)})())
6.使用,打开英文网页:
7.点击有道翻译2.0,成功翻译网页:
先给一个JS随机数发生器函数:(别告诉我你知道怎么调用……)<script language="JavaScript"><!--
// The Central Randomizer 1.3 (C) 1997 by Paul Houle ([email protected])
// See: http://www.msc.cornell.edu/~houle/javascript/randomizer.html
rnd.today=new Date()
rnd.seed=rnd.today.getTime()
function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280
return rnd.seed/(233280.0)
}
function rand(number) {
return Math.ceil(rnd()*number)
}
// end central randomizer. -->
</script>
这样就可以用rand(6)来反复生成一个1到6之间的随机数,生成了几就选出那个位置上的数,如果该位置上的数已经被选出来就再生成一个随机数,直到取出了4个位置上的数为止~~
还有什么疑问么~~?