composer install命令出错-->
1 2 3 |
[Composer\Downloader\TransportException] Your configuration does not allow connections to http://packagist.phpcomposer.com/packages.json. See https://getcom poser.org/doc/06-config.md#secure-http for details. |
解决方法:因为镜像使用用的是http,而原地址是需要https,所以配置下关掉https就好了。
1 2 3 4 5 |
{ "config": { "secure-http": false } } |
报错信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Warning: Accessing packagist.phpcomposer.com over http which is an insecure protocol. Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages. Problem 1 - laravel/framework v5.3.9 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.8 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.7 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.6 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.5 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.4 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.3 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.2 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.18 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.17 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.16 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.15 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.14 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.13 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.12 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.11 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.10 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.1 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - laravel/framework v5.3.0 requires ext-openssl * -> the requested PHP extension openssl is missing from your system. - Installation request for laravel/framework 5.3.* -> satisfiable by laravel/framework[v5.3.0, v5.3.1, v5.3.10, v5.3.11, v5.3.12, v5.3.13, v5.3.14, v5.3.15, v5.3.16, v5.3.17, v5.3.18, v5.3.2, v5.3.3, v5.3.4, v5.3.5, v5.3.6, v5.3.7, v5.3.8, v5.3.9]. |
解决方案:修改php.ini开启openssl拓展 Laravel 出现"RuntimeException inEncrypter.php line 43: The only supported ciphers are AES-128-CBC and AES-256-CBC with the correct key lengths."问题的解决办法——> 首先在cmd命令行下定位到项目所在的根目录下,接着输入:
1 |
php artisan key:generate |
这时候项目根目录下的.env文件里的APP_KEY应该会有值了: 若没有,将上面的key输入进去 from:http://www.cnblogs.com/vincebye/p/5947739.html
View Details平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过spring容器帮我们new指定实例并且将实例注入到需要该对象的类中。依赖注入的另一种说法是“控制反转”,通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员,而控制反转是指new实例工作不由我们程序员来做而是交给spring容器来做。 spring有多种依赖注入的形式,下面仅介绍spring通过xml进行IOC配置的方式: Set注入 这是最简单的注入方式,假设有一个SpringAction,类中需要实例化一个SpringDao对象,那么就可以定义一个private的SpringDao成员变量,然后创建SpringDao的set方法(这是ioc的注入入口): Java代码 package com.bless.springdemo.action; public class SpringAction { //注入对象springDao private SpringDao springDao; //一定要写被注入对象的set方法 public void setSpringDao(SpringDao springDao) { this.springDao = springDao; } public void ok(){ springDao.ok(); } } 随后编写spring的xml文件,<bean>中的name属性是class属性的一个别名,class属性指类的全名,因为在SpringAction中有一个公共属性Springdao,所以要在<bean>标签中创建一个<property>标签指定SpringDao。<property>标签中的name就是SpringAction类中的SpringDao属性名,ref指下面<bean name="springDao"…>,这样其实是spring将SpringDaoImpl对象实例化并且调用SpringAction的setSpringDao方法将SpringDao注入: Java代码 <!--配置bean,配置后该类由spring管理--> <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> <!--(1)依赖注入,配置当前类中相应的属性--> <property name="springDao" ref="springDao"></property> </bean> <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean> 构造器注入 这种方式的注入是指带有参数的构造函数注入,看下面的例子,我创建了两个成员变量SpringDao和User,但是并未设置对象的set方法,所以就不能支持第一种注入方式,这里的注入方式是在SpringAction的构造函数中注入,也就是说在创建SpringAction对象时要将SpringDao和User两个参数值传进来: Java代码 public class SpringAction { //注入对象springDao private SpringDao springDao; private User user; public SpringAction(SpringDao springDao,User user){ this.springDao = springDao; this.user = user; System.out.println("构造方法调用springDao和user"); } public void save(){ user.setName("卡卡"); springDao.save(user); } } 在XML文件中同样不用<property>的形式,而是使用<constructor-arg>标签,ref属性同样指向其它<bean>标签的name属性: Xml代码 <!--配置bean,配置后该类由spring管理--> <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> <!--(2)创建构造器注入,如果主类有带参的构造方法则需添加此配置--> <constructor-arg ref="springDao"></constructor-arg> <constructor-arg ref="user"></constructor-arg> </bean> <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean> <bean name="user" class="com.bless.springdemo.vo.User"></bean> 解决构造方法参数的不确定性,你可能会遇到构造方法传入的两参数都是同类型的,为了分清哪个该赋对应值,则需要进行一些小处理: 下面是设置index,就是参数位置: Xml代码 <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> <constructor-arg index="0" ref="springDao"></constructor-arg> <constructor-arg index="1" ref="user"></constructor-arg> </bean> 另一种是设置参数类型: Xml代码 <constructor-arg type="java.lang.String" ref=""/> 静态工厂的方法注入 静态工厂顾名思义,就是通过调用静态工厂的方法来获取自己需要的对象,为了让spring管理所有对象,我们不能直接通过"工程类.静态方法()"来获取对象,而是依然通过spring注入的形式获取: Java代码 package com.bless.springdemo.factory; import com.bless.springdemo.dao.FactoryDao; import com.bless.springdemo.dao.impl.FactoryDaoImpl; import com.bless.springdemo.dao.impl.StaticFacotryDaoImpl; public class DaoFactory { //静态工厂 public static final FactoryDao getStaticFactoryDaoImpl(){ return new StaticFacotryDaoImpl(); } } 同样看关键类,这里我需要注入一个FactoryDao对象,这里看起来跟第一种注入一模一样,但是看随后的xml会发现有很大差别: Java代码 […]
View Details最近几天在看一本名为Dependency Injection in .NET 的书,主要讲了什么是依赖注入,使用依赖注入的优点,以及.NET平台上依赖注入的各种框架和用法。在这本书的开头,讲述了软件工程中的一个重要的理念就是关注分离(Separation of concern, SoC)。依赖注入不是目的,它是一系列工具和手段,最终的目的是帮助我们开发出松散耦合(loose coupled)、可维护、可测试的代码和程序。这条原则的做法是大家熟知的面向接口,或者说是面向抽象编程。 关于什么是依赖注入,在Stack Overflow上面有一个问题,如何向一个5岁的小孩解释依赖注入,其中得分最高的一个答案是: “When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open, you might get something Mommy or Daddy doesn’t want you to have. You might even be looking for something we don’t even have or which has expired. What you should be doing is stating a need, “I need something to drink with lunch,” and then we will make sure you have something when you sit […]
View Details在Java web项目中经常出现重命名一个Java文件,最后不能运行,原因是tomcat服务器有缓存。解决办法是进行clean。 在一个项目中,如果我们想要对全部类文件及配置文件进行重新编译,在Eclipse可以采取如下办法: 在Eclipse下,选中Project下的Clean一项,进入之后可以看到Clean all projects选后点“OK”就可以了。这时我们再打开Eclipse中用来保存编译文件的classes文件就可以看到刚才编译的。 from:http://blog.csdn.net/gao1440156051/article/details/53517197
View DetailsEclipse的web工程至Tomcat默认的部署目录是在工程空间下,本文旨在将部署目录改为Tomcat安装目录,并解决依赖包输出问题。 1.在Eclipse中添加Tomcat服务器。 2.将web工程发布至tomcat: 选择刚添加的Tomcat: 此时Eclipse将自动生成Servers工程: 3.在Servers视图,Remove删除刚刚发布的项目: 4.打开Tomcat服务器配置项: 5.修改以下两个配置项,Tomcat保持启动状态,否则Server Locations一栏变灰色,不可用: 6.配置lib输出。 项目右键: 点击下一步,选中需要随项目发布到tomcat的包,完成确认操作。 7.修改工程下的.classpath文件,将如下配置项提至最前面,否则发布项目时,先前发布到WEB-INF里面的文件将被覆盖: 8.重新发布项目即可。 from:http://blog.csdn.net/lucklq/article/details/7621807
View DetailsIIS是Internet Information Services(互联网信息服务)的缩写,是由微软公司提供的基于运行Microsoft Windows的互P联网基本服务,是微软在Windows中内置的Web服务器软件。通过IIS和Tomcat的集成可以让Tomcat处理JSP,IIS处理ASP和Html静态页面。 一、安装tomcat 我所用的Tomcat是最新版的7.0.27直接安装版,安装的位置是D:\Program Files\Tomcat7,记住这个路径会多次用到,注意在Tomcat之前一定要安装JDK,这里我用的是jdk1.7. 二、配置IIS 进入Win7的控制面板,打开程序功能,点击选择打开或关闭Windows功能 在Windows功能的选项菜单,把internet信息服务的所有的组件全部勾起来。 注意:安装完成后IIS的地址是http://localhost和tomcat的地址http://localhost:8080看看出没出现默认画面。 三、集成Tomcat 1、连接器jk 在tomcat的bin目录下新建目录jk\,把下载的isapi_redirect.dll(连接器,可从网上下载http://mirror.bit.edu.cn/apache//tomcat/tomcat-connectors/jk/binaries/windows/,我使用的是目前的最新版1.2.35),拷贝到这个目录下。 2、在tomcat的conf目录下创建两个文件workers.properties,uriworkermap.properties Workers.properties文件的内容:
1 2 3 4 5 6 7 8 |
workers.tomcat_home=D:\Program Files\Tomcat7 workers.java_home=D:\Program Files\Java\jdk1.7 ps=\ worker.list=worker1 worker.worker1.port=8009 worker.worker1.host=localhost worker.worker1.type=ajp13 worker.worker1.lbfactor=1 |
uriworkermap.properties文件的内容(注意:这个文件说明了符合哪些条件的uri才能被转发到tomcat中,可能的设置有)
1 2 3 4 |
/*=worker1 /*.jsp=worker1 /jsp-example/*=worker1 !/*.gif=worker1 |
3、导入注册表文件tomcat.reg 任意的在哪里建一个文本文档,命名为tomcat.reg,内容如下:
1 2 3 4 5 6 7 |
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Jakarta Isapi Redirector\1.0] "extension_uri"="/jakarta/isapi_redirect.dll" "log_file"="D:\\Program Files\\Tomcat7\\logs\\isapi.log" "worker_file"="D:\\Program Files\\Tomcat7\\conf\\workers.properties" "worker_mount_file"="D:\\Program Files\\Tomcat7\\conf\\uriworkermap.properties" "log.level"="realease" |
双击运行这个文件导入到注册表中。 4、建立网站的虚拟目录 打开你的Internet信息服务管理器,进入管理页面,展开左边的连接栏,选择你的一个网站,这里我的网站名是tomcat,主目录是e:\tomcat,鼠标单击右键添加虚拟目录,打开虚拟目录对话框。 别名处写上jakarta,其中jakarta是一个规定名字,必须这么创建,这个虚拟目录要指向isapi_redirect.dll的所在目录,并且设置为可执行IsApi。 5、添加筛选器 选中自己的网站,选择ISAPI筛选器,如图所示: 在出现的框中,单击鼠标右键,单击添加选项,出现添加ISAPI筛选器对话框,在名称处中输入jakarta,并选择连接器的路径,如下图所示: 6、添加脚本映射 单击“程序处理映射”选项,在打开的对话框中鼠标右键单击“添加脚本映射”选项,在打开的对话框中,按下图的配置进行输入: 在弹出的对话框中单击是按钮。 之后选择jakarta这项,单击鼠标右键选择编辑功能权限选项,如下图所示全部选中。至此所有的配置完毕。 在浏览器中输入“http://localhost”,在IE中有时如果不加HTTP的化可能会出现错误,这样(不加8080)访问IIS的服务器,可以看到网页显示的是tomcat的默认主页,说明IIS已经指向了tomcat,为了能更好的服务最好将两个服务器指向同一个的Web的目录,这样的IIS就支持jsp了,完成之后重新启动。 在tomcat的配置文件server.xml文件中添加以下这句话可以更改tomcat的默认目录: <Context path=”” docBase=”e:\tomcat”(此处为你的网站根目录,通过这句话的映射,可以将原先的默认更改为新的目录) debug=”0”/> 以上的操作是我根据网上的,书上的做的总结。在百度文库中我也发了一份,我也是初级的研究,如有不足之处希望大家理解。希望大家给我评论一下。 from:http://blog.csdn.net/longyuhome/article/details/7577502
View Details因為想要在一台電腦上同時執行 ASPX 與 JSP 而只能使用80 PORT, 所以只能透過IIS 來轉發 request到tomcat 使用的Tomcat版本是8.0 方法如下: 1.下載 The Apache Tomcat Connectors 我下載的版本是1.2.40 可以得到 isapi_redirect.dll , 我是放在 c:\tomcat8\bin\ 2.設定connector 根據官網的文件 isapi_redirect 的設定方式有兩種 (1) registry setting 在HKLM\SOFTWARE\Apache Software Foundation\Jakarta Isapi Redirector\1.0 新增最少以下資訊 extension_uri:/jakarta/isapi_redirect.dll (固定值) log_file: c:/tomcat8/logs/isapi_redirect.log log_level: info worker_file: c:/tomcat8/conf/workers.properties (workers設定檔的位置) worker_mount_file: C:/tomcat8/conf/uriworkermap.properties (uri對照的mapping) (2)isapi_redirect.properties 設定同上, 不過我試的結果是沒有用, 所以用第一種方法 #固定值 extension_uri=/jakarta/isapi_redirect.dll #log檔放的位置可以依日期來當檔名可以參考官網設定 log_file=c:/tomcat8/logs/isapi_redirect.log #log的記錄內容有 debug, info, warn, error, trace log_level=info #worker設定檔的位置 worker_file=c:/tomcat8/conf/workers.properties #worker uri mapping對照檔的位置 worker_mount_file=C:/tomcat8/conf/uriworkermap.properties 3.建立workers.properties文件 官網worker參數設定 #隨便設定, 但是要跟等等設定的 uriworkermap.properties一致就好 worker.list=jspWorker #可以是ajp13, ajp14, jni, lb 或是 status #不過以下的設定需要跟server.xml的connector的設定一致 worker.geloinWorker.type=ajp13 #tomcat server所在的電腦 worker.geloinWorker.host=localhost worker.geloinWorker.port=8009 4.設定uriworkermap.properties 詳細設定要參考官網的uri設定 這邊要跟workers.properties的 worker.list設定一致 /*.jsp=jspWorker […]
View Details刚进公司,不是很熟悉maven,总是访问不了项目,总是报404的错; 解决如下; 选中“项目", 然后右击选择“properties”—->Deployment Asssembly, 然后将webContent项remove掉,还有test相关的文件也可以remove掉, test是测试相关的文件, add一个folder文件,next-->next-->src下的main下的webapp文件,最后击“Finish”, 在add一个Java Build Path Entries,next—>Maven Dependencies文件,最后再点击"Finish"; 最后再点击"OK";重新启动tomcat,在浏览器中输入相应的地址:http://localhost:8080/MavenTest/index.jsp ,进行测试web项目是否创建成功。 //tomcat 没有jar问题
1 2 3 4 |
如果你是maven项目,tomcat在发布项目的时候没有同时发布maven依赖所添加的jar包, 你需要设置一下eclipse: 项目 —> 属性 -> Deployment Assembly -> Add -> Java Build Path Entries -> 选择Maven Dependencies -> Finish -> OK 把对应的Maven依赖包也发布到tomcat,调试时会自动把那些jar发布到指定目录下,tomcat也能找到那些jar了。 |
from:http://blog.csdn.net/my_name_nb/article/details/59112305
View DetailsMaven是当前流行的项目管理工具,但官方的库在国外经常连不上,连上也下载速度很慢。国内oschina的maven服务器很早之前就关了。今天发现阿里云的一个中央仓库,亲测可用。
1 2 3 4 5 6 |
<mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> </mirror> |
修改${maven.home}/conf或者${user.home}/.m2文件夹下的settings.xml文件,在<mirrors>标签下加入上述内容即可。如下: <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <mirrors> <!--阿里云仓库--> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> </mirror> <!--中央仓库1--> <mirror> <id>repo1</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo1.maven.org/maven2/</url> </mirror> <!--中央仓库2--> <mirror> <id>repo2</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo2.maven.org/maven2/</url> </mirror> </mirrors> </settings> from:http://www.cnblogs.com/xiongxx/p/6057558.html
View Details一、java.lang.Math.Random; 调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取值范围是[0.0,1.0)的左闭右开区间,返回值是一个伪随机选择的数,在该范围内(近似)均匀分布。例子如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package IO; import java.util.Random; public class TestRandom { public static void main(String[] args) { // 案例1 System.out.println("Math.random()=" + Math.random());// 结果是个double类型的值,区间为[0.0,1.0) int num = (int) (Math.random() * 3); // 注意不要写成(int)Math.random()*3,这个结果为0,因为先执行了强制转换 System.out.println("num=" + num); /** * 输出结果为: * * Math.random()=0.02909671613289655 * num=0 * */ } } |
二、java.util.Random 下面Random()的两种构造方法: Random():创建一个新的随机数生成器。 Random(long seed):使用单个 long 种子创建一个新的随机数生成器。 我们可以在构造Random对象的时候指定种子(这里指定种子有何作用,请接着往下看),如:Random r1 = new Random(20); 或者默认当前系统时间的毫秒数作为种子数:Random r1 = new Random(); 需要说明的是:你在创建一个Random对象的时候可以给定任意一个合法的种子数,种子数只是随机算法的起源数字,和生成的随机数的区间没有任何关系。如下面的Java代码:
1 2 3 |
Random rand =new Random(25); int i; i=rand.nextInt(100); |
初始化时25并没有起直接作用(注意:不是没有起作用),rand.nextInt(100);中的100是随机数的上限,产生的随机数为0-100的整数,不包括100。 具体用法如下例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package IO; import java.util.ArrayList; import java.util.Random; public class TestRandom { public static void main(String[] args) { // 案例2 // 对于种子相同的Random对象,生成的随机数序列是一样的。 Random ran1 = new Random(10); System.out.println("使用种子为10的Random对象生成[0,10)内随机整数序列: "); for (int i = 0; i < 10; i++) { System.out.print(ran1.nextInt(10) + " "); } System.out.println(); Random ran2 = new Random(10); System.out.println("使用另一个种子为10的Random对象生成[0,10)内随机整数序列: "); for (int i = 0; i < 10; i++) { System.out.print(ran2.nextInt(10) + " "); } /** * 输出结果为: * * 使用种子为10的Random对象生成[0,10)内随机整数序列: * 3 0 3 0 6 6 7 8 1 4 * 使用另一个种子为10的Random对象生成[0,10)内随机整数序列: * 3 0 3 0 6 6 7 8 1 4 * */ // 案例3 // 在没带参数构造函数生成的Random对象的种子缺省是当前系统时间的毫秒数。 Random r3 = new Random(); System.out.println(); System.out.println("使用种子缺省是当前系统时间的毫秒数的Random对象生成[0,10)内随机整数序列"); for (int i = 0; i < 10; i++) { System.out.print(r3.nextInt(10)+" "); } /** * 输出结果为: * * 使用种子缺省是当前系统时间的毫秒数的Random对象生成[0,10)内随机整数序列 * 1 1 0 4 4 2 3 8 8 4 * */ // 另外,直接使用Random无法避免生成重复的数字,如果需要生成不重复的随机数序列,需要借助数组和集合类 ArrayList list=new TestRandom().getDiffNO(10); System.out.println(); System.out.println("产生的n个不同的随机数:"+list); } /** * 生成n个不同的随机数,且随机数区间为[0,10) * @param n * @return */ public ArrayList getDiffNO(int n){ // 生成 [0-n) 个不重复的随机数 // list 用来保存这些随机数 ArrayList list = new ArrayList(); Random rand = new Random(); boolean[] bool = new boolean[n]; int num = 0; for (int i = 0; i < n; i++) { do { // 如果产生的数相同继续循环 num = rand.nextInt(n); } while (bool[num]); bool[num] = true; list.add(num); } return list; } } |
备注:下面是Java.util.Random()方法摘要: protected int next(int bits):生成下一个伪随机数。 boolean nextBoolean():返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的boolean值。 void nextBytes(byte[] bytes):生成随机字节并将其置于用户提供的 byte 数组中。 double nextDouble():返回下一个伪随机数,它是取自此随机数生成器序列的、在0.0和1.0之间均匀分布的 double值。 float nextFloat():返回下一个伪随机数,它是取自此随机数生成器序列的、在0.0和1.0之间均匀分布float值。 double nextGaussian():返回下一个伪随机数,它是取自此随机数生成器序列的、呈高斯(“正态”)分布的double值,其平均值是0.0标准差是1.0。 int nextInt():返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。 int nextInt(int n):返回一个伪随机数,它是取自此随机数生成器序列的、在(包括和指定值(不包括)之间均匀分布的int值。 long nextLong():返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 long 值。 void setSeed(long seed):使用单个 long 种子设置此随机数生成器的种子。 下面给几个例子: 生成[0,1.0)区间的小数:double d1 = r.nextDouble(); 生成[0,5.0)区间的小数:double d2 = r.nextDouble() * 5; 生成[1,2.5)区间的小数:double d3 = r.nextDouble() * 1.5 + 1; 生成-231到231-1之间的整数:int n = r.nextInt(); 生成[0,10)区间的整数: int n2 = r.nextInt(10);//方法一 n2 = Math.abs(r.nextInt() % 10);//方法二 参考资料: http://blog.sina.com.cn/s/blog_93dc666c0101h3gd.html http://blog.csdn.net/wpjava/article/details/6004492
View Details