Math.random 重复随机,不重复随机

package random;

public class RandomRepeatOrNotRepeat {
	// 随机数字最大值
	public final static int maxValue = 11;
	// 随机数字最小值
	public final static int minValue = 1;
	// 需要几位数字
	public final static int resultLength = 5;
	// 生成多少行
	public final static int rows = 10;

	public static void main(String args[]) {
		// 不重复随机,将已经随机到的下标移动到最后一个,减少数组长度
		three();
		// 不重复随机,标记数组中的值
		// two();
		// 普通随机
		// one();

	}

	public static void three() {
		int[] a = new int[maxValue];
		int[] result = new int[resultLength];

		for (int j = 0; j < rows; j++) {
			a = new int[maxValue];
			init(a);
			for (int i = 0; i < resultLength; i++) {
				int random = (int) (Math.random() * a.length);
				swap2tail(a, random);

				result[i] = a[a.length - 1];
				a = decreaseLength(a);
			}
			for (int i = 0; i < result.length; i++)
				System.out.print(result[i] + ",");
			System.out.println();
		}
	}

	private static int[] decreaseLength(int[] a) {
		int[] result = new int[a.length - 1];
		for (int i = 0; i < result.length; i++)
			result[i] = a[i];
		return result;
	}

	private static void swap2tail(int[] a, int random) {
		if (random == a.length - 1)
			return;
		a[random] ^= a[a.length - 1];
		a[a.length - 1] ^= a[random];
		a[random] ^= a[a.length - 1];
	}

	private static void init(int[] a) {
		for (int i = 0; i < a.length; i++)
			a[i] = i + minValue;
	}

	public static void two() {
		for (int j = 0; j < rows; j++) {
			int[] a = new int[maxValue];
			for (int i = 0; i < resultLength; i++) {
				int random = (int) (Math.random() * maxValue);
				markA(a, random);
			}
			printValueNotOne(a);
		}

	}

	private static void printValueNotOne(int[] a) {
		for (int i = 0; i < a.length; i++) {
			if (a[i] != 0)
				System.out.print(i + minValue + ",");
		}
		System.out.println();
	}

	public static void markA(int[] a, int random) {
		if (a[random] == 0) {
			a[random] = 1;
			return;
		}

		markA(a, (random + 1) % a.length);

	}

	public static void one() {
		String s = "";
		for (int j = 0; j < rows; j++) {
			for (int i = 0; i < resultLength; i++) {
				s += ((int) (Math.random() * maxValue + minValue)) + ",";
			}
			System.out.println(s);
			s = "";
		}
	}

	public static void printTestArray(int[] a) {
		System.out.println("----------- Test -----------");
		for (int i = 0; i < a.length; i++)
			System.out.print(a[i] + ",");
		System.out.println();

		System.out.println("----------- Test -----------");
	}

}

时间: 2024-08-07 16:44:30

Math.random 重复随机,不重复随机的相关文章

Math.random()的使用方法

一.Math.random()的介绍 Math.radom()可以获得一个0.0到1.0的随机double值,不包括1.0,即[0.0,1.0). 二.使用方法 场景1:随机生成[0.0,11.0) 场景2:随机生成[10.0,15.0] 场景3:随机生成[10.0,15.0] 三.总结 1.随机产生[0.0,1.0) Math.random(); 2.随机产生[0,M) Math.random()*M; 3.随机产生[M,N) m+Math.random()*(n-m): 4.随机产生[M,N

react random key generator;react如何产生随机不重复的key

1.<div key={+new Date() + Math.random()}> 2.使用数组的索引 3.使用uuid:https://www.npmjs.com/package/uuid 4.使用uniqid:https://www.npmjs.com/package/uniqid 5.Date.now() 原文地址:https://www.cnblogs.com/shengulong/p/10122759.html

获取一个1~50的随机不重复数组

// 获取一个1~50的随机不重复数组// Math.ceil()用于向上取整 Math.floor()用于向下取整 Math.round()用于四舍五入取整数var arr = [];var number = 50;for(var i=1;i<=number;i++){ arr.push(i);}console.log(arr);var result = [];for(var j=number-1;j>=0;j--){ var rand = Math.ceil(Math.random()*j

生成四位随机不重复验证码

1.生成四位随机不重复验证码 //生成一个四位不重复的随机码 public static String get4Codes(){ StringBuffer sb = new StringBuffer(); String codes = "qwertyuiopasdfghjklzxcvbnm0123456789"; Random random = new Random(); for (int i =0; i< codes.length(); i++){ char c = codes

随机函数Math.random()_随机数,随机排序

Math.random() 返回0到1之间的随机数(小数) 如:0.6417997585228452 通过Math.random()和sort函数可实现数组的随机排序,代码如下: 1 arr.sort(function( a, b ){ 2 return Math.random() - 0.5; 3 }); 4 5 alert( arr ); //8,7,4,3,2,1,5,6 总结产生随机数的公式: x ~ y 产生x到y之间的随机整数Math.round( Math.random()*(y-

Android使用java的Math.Random获取随机色值

网上有个ColorPicker开源项目,选择颜色值.而在这里我想实现的是动态修改一个view的背景色. 开一个线程,每隔1s修改一次view的背景色. 我们知道在android里设置一个view的背景色有 framelayout.setBackgroundColor(Color.parseColor("#"+arg0.obj)); 就是用android的Color类去解析一个带#号的十六进制色值.现在要做的是动态随机获取这个16进制色值. java给我们提供了两种方法获取随机数: 一.

使用js Math.random()函数生成n到m间的随机数字

何使用js生成n到m间的随机数字,主要目的是为后期的js生成验证码做准备,Math.random()函数返回0和1之间的伪随机数 摘要: 本文讲解如何使用js生成n到m间的随机数字,主要目的是为后期的js生成验证码做准备. Math.random()函数返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) 1.生成n-m,包含n但不包含m的整数: 第一步算出 m-n的值,假设等于w 第二步Math.random()*w 第三步Math.random()*w+n 第四步parseInt(M

【学习笔记】使用Math.floor与Math.random取随机整数的方法详解

Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result. (小于等于 x,且与 x 最接近的整数.)其实返回值就是该数的整数位:Math.floor(0.666)   -->  0Math.floor(39.2783)   -->  39 所以我们可以使用Math.floor(Math.random())去获取你想要的一

关于数组随机不重复的思路

例如双色球的红色举例: 首先知道数组的长度 6个:利用循环里的数组长度小于6进行循环: //封装产生随机数的函数 function randow(n,m){ return parsentInt(Math.random() * (m-n) + n) } //定义一个空数组 var arr=[]: //封装一个判断新随机数是否于之前重复: function arrinfo(num,arr){ for(var i=0;i<arr.length;i++){ if(num == arr[i]){ retu