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 -----------");
	}

}


可以使用JavaScript的Math.random()函数来生成随机数。Math.random()函数没有任何参数,它会返回一个介于0和1之间的伪随机数,包括0但不包括1。 通过Math.random()函数结合Math.floor()方法,可以得到不同范围内的随机整数。例如,要生成0到9之间的随机整数,可以使用Math.floor(Math.random() * 10)。如果想生成1到100之间的随机整数,可以使用Math.floor(Math.random() * 100) + 1。 请注意,Math.random()函数生成的是伪随机数,它是基于算法的,并不是真正的随机数。因此,在某些情况下,可能会出现一定的重复性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [JavaScript随机数的方法Math.random()](https://blog.csdn.net/m0_58473847/article/details/131232561)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [javascript Math.random()随机数函数](https://download.csdn.net/download/weixin_38521169/14812810)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值