这样输出“Hello World”
来源:互联网 发布:mac samba客户端 编辑:程序博客网 时间:2024/06/07 20:41
import java.util.Random;/** * Created by admin on 2017/8/29. */public class HelloWorld { public static String randomString(int i){ Random random = new Random(i); StringBuffer stringBuffer = new StringBuffer(); for (int n = 0; ; n++) { int k = random.nextInt(27); if (k==0){ break; } stringBuffer.append((char)('`' + k)); } return stringBuffer.toString(); } public static void main(String[] args) { System.out.print(randomString(-229985452)+" "+randomString(-147909649)); }}
为什么会输出hello world?
通过这句命令首先得到的六个数是:
8 5 12 12 15 0
然后,通过new Random(-147909649).nextInt(27)得到的6个数是:
23 15 18 12 4 0
这样一来,根据:sb.append((char) (‘`’+n)); 得到:
8 + 96 = 104 –> h
5 + 96 = 101 –> e
12 + 96 = 108 –> l
12 + 96 = 108 –> l
15 + 96 = 111 –> o
23 + 96 = 119 –> w
15 + 96 = 111 –> o
18 + 96 = 114 –> r
12 + 96 = 108 –> l
4 + 96 = 100 –> d
整个程序的执行就是这样。
- 关于这个程序的运行原理的解释
“如果java.util.Random是被一个具体的数字做为“随机数种子”而实例化(在本例中是-229985452和-147909649),那么该实例就会以这个随机数种子作为随机算法产生随机数的基础。
使用同样的种子实例化的Random对象,每次运行时将会遵循同一种模式,产生同样的序列。”
这就是为什么每次运行该程序都会产生同样的结果的原理啦~
- 扩展阅读:计算机伪随机数问题
随机数在计算机科学和计算机应用中都扮演非常重要的角色(例如通信、安全、密码等方面)。但是,由于计算机本身的结构和原理,实际上是无法产生出所谓的“绝对随机数”的。也就是说,计算机所产生的随机数,实际上是有某种规律或者模式的“伪随机数”(Pseudo random number)。
如何用计算机程序产生高质量随机数,这是计算机理论科学中的一个非常重要的课题。尤其是在复杂的计算环境下的高质量随机数的产生,需要牵涉到非常高深的计算科学和数学方面的理论研究。
- 这样输出“Hello World”
- 输出"HELLO WORLD" ASM
- 汇编程序输出“Hello world”
- django输出 hello world
- php 输出Hello World
- 页面输出Hello World
- Java:输出Hello World!
- 1.5 输出Hello World
- python输出Hello World
- 输出hello world
- 将“hello world” 输出为“world hello”
- hello world 输出 olleh dlrow
- PHP之输出"hello World"
- C#控制台 输出hello world
- 使用vuejs输出Hello world
- 使用reactjs输出Hello world
- 1.1-1 输出Hello World!
- 用JSP输出Hello World
- 电子概念——液晶显示器重要参数检测
- IO流模板
- 设置ListView的高度(重算组件子项)
- inline-block兼容低版本IE浏览器~~在IE6/Ie7和IE8中
- android studio cmake添加jsoncpp
- 这样输出“Hello World”
- Qt删除TreeWidget节点
- Android Studio打开DDMS : An error has occurred URIUtil
- 中文查询乱码解决过程记录
- Array对象的方法实现(3)----Array.prototype.filter和Array.prototype.find(实现常规参数的功能)
- PAT (Basic Level) Practise (中文) 1007. 素数对猜想 (20)
- 通过maven-shade-plugin 解决Elasticsearch与spark的jar包冲突问题
- HashMap、HashTable的key和value是否可为null 或者 ""
- 【LeetCode】415. Add Strings