java生成随机数方法大全(可指定范围、分布方式、类型、种子)

b4c2的头像 b4c2 31 2014-12-31 22:56 1

 基本信息

× 1   

浏览数: 7388

分享时间: 4 年 前

11
 
import java.util.Random;

/**
 * Java实用工具类库中的类java.util.Random提供了产生各种类型随机数的方法。
 * 它可以产生int、long、float、double以及Goussian等类型的随机数。
 * 
 * java.lang.Math中的方法random()只产生double型的随机数。
 */
public class RandomNumber {

	public static void main(String[] args) {

		// 使用java.lang.Math的random方法生成随机数
		System.out.println("Math.random(): " + Math.random());

		// 使用不带参数的构造方法构造java.util.Random对象
		System.out.println("使用不带参数的构造方法构造的Random对象:");
		Random rd1 = new Random();
		// 产生各种类型的随机数
		// 按均匀分布产生整数
		System.out.println("int: " + rd1.nextInt());
		// 按均匀分布产生长整数
		System.out.println("long: " + rd1.nextLong());
		// 按均匀分布产生大于等于0,小于1的float数[0, 1)
		System.out.println("float: " + rd1.nextFloat());
		// 按均匀分布产生[0, 1)范围的double数
		System.out.println("double: " + rd1.nextDouble());
		// 按正态分布产生随机数
		System.out.println("Gaussian: " + rd1.nextGaussian());

		// 指定随机数产生的范围
		System.out.print("[0,10)范围内随机整数序列: ");
		for (int i = 0; i < 10; i++) {
			// Random的nextInt(int n)方法返回一个[0, n)范围内的随机数
			System.out.print(rd1.nextInt(10) + "  ");
		}
		System.out.println();
		System.out.print("[5,23)范围内随机整数序列: ");
		for (int i = 0; i < 10; i++) {
			// 因为nextInt(int n)方法的范围是从0开始的,
			// 所以需要把区间[5,28)转换成5 + [0, 23)。
			System.out.print(5 + rd1.nextInt(23) + "  ");
		}
		System.out.println();
		System.out.print("利用nextFloat()生成[0,99)范围内的随机整数序列: ");
		for (int i = 0; i < 10; i++) {
			System.out.print((int) (rd1.nextFloat() * 100) + "  ");
		}
		System.out.println();
		System.out.println();

		// 使用带参数的构造方法构造Random对象
		// 构造函数的参数是long类型,是生成随机数的种子。
		System.out.println("使用带参数的构造方法构造的Random对象:");
		Random ran2 = new Random(10);
		// 对于种子相同的Random对象,生成的随机数序列是一样的。
		System.out.println("使用种子为10的Random对象生成[0,10)内随机整数序列: ");
		for (int i = 0; i < 10; i++) {
			System.out.print(ran2.nextInt(10) + "  ");
		}
		System.out.println();
		Random ran3 = new Random(10);
		System.out.println("使用另一个种子为10的Random对象生成[0,10)内随机整数序列: ");
		for (int i = 0; i < 10; i++) {
			System.out.print(ran3.nextInt(10) + "  ");
		}
		System.out.println();
		// ran2和ran3生成的随机数序列是一样的,如果使用两个没带参数构造函数生成的Random对象,
		// 则不会出现这种情况,这是因为在没带参数构造函数生成的Random对象的种子缺省是当前系统时间的毫秒数。

		// 另外,直接使用Random无法避免生成重复的数字,如果需要生成不重复的随机数序列,需要借助数组和集合类

	}
}


12 3 4 [下一页]

  • ossaa的头像 ossaa 2018-12-15 04:56 代码数:0

    I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business. click here

  • ossaa的头像 ossaa 2018-12-15 05:04 代码数:0

    I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business. click here

  • cixexey的头像 cixexey 2018-06-22 19:32 代码数:0

    Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article.Simple to get Discount term Life Insurance policies online

  • ossaa的头像 ossaa 2018-12-19 04:21 代码数:0

    I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well! automotive clear coat

  • wzg356的头像 wzg356 2015-01-03 18:23 代码数:0
    先收集,后学习
  • jasonchip的头像 jasonchip 2018-10-21 23:23 代码数:0

    Hi! Thanks for the great information you have provided! Finally a solution to my swim school website and the phone connection direct calling or whatsapp. 

  • cixexey的头像 cixexey 2018-06-26 18:31 代码数:0

    Great Article it its really informative and innovative keep us posted with new updates. its was really valuable. thanks a lot.local web design

  • ossaa的头像 ossaa 2018-12-22 16:51 代码数:0

    This article gives the light in which we can observe the reality. This is very nice one and gives in depth information. Thanks for this nice article.

    Tax Preparation Tucson

  • cixexey的头像 cixexey 2018-06-27 23:38 代码数:0

    This is such a great resource that you are providing and you give it away for free.Ulasan sepatu terbaru dan terbaik

  • ossaa的头像 ossaa 2018-12-22 17:35 代码数:0

    All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks

    Sports doctor Lubbock

您的评论: