递归实现随机数不重复问题

今天朋友求助,要求随机5个数,并且不能重复,其实这个写起来蛮简单的,我的第一想法就是递归实现,可是最后在一个小地方卡了快半个小时,把我整的汗流浃背的,最终还是我胜利了,解决了,我把代码贴出来.

public class Random {

	public static int[] randoms = new int[5];

	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			randoms[i] = getRandom();
			System.out.print(randoms[i] + "|");
		}
	}

	// 获取单个随机数
	static int random =0;
	public static int getRandom() {
		random= (int) (Math.random() * 5 + 1);
		for (int j = 0; j < randoms.length; j++) {
			if (random == randoms[j]) {
				getRandom();
			}
		}
		return random;
	}
}
原文地址:https://www.cnblogs.com/Laupaul/p/2338682.html