我正在用Java写一个MasterMind程序。我的意图是生成一个4位数字,但所有数字都需要不同。你会如何使用Math.random()
?或者,还有更好的方法?如何生成一个不重复的随机数
例如:这里
4321 (allowed)
4341 (not allowed)
我正在用Java写一个MasterMind程序。我的意图是生成一个4位数字,但所有数字都需要不同。你会如何使用Math.random()
?或者,还有更好的方法?如何生成一个不重复的随机数
例如:这里
4321 (allowed)
4341 (not allowed)
使用集合,以确定你已经有这个数字:
import java.util.ArrayList;
public class MyRandom {
public static void main(String[] args) {
System.out.println(getRandom(4));
System.out.println(getRandom(4));
System.out.println(getRandom(10));
}
public static String getRandom(int length){
if (length>10) return "Hexadecimal?";
ArrayList<Integer> numbers=new ArrayList<Integer>();
while (length>0){
int digit=(int)(Math.random()*10);
if (numbers.contains(digit)) continue;
numbers.add(digit);
length--;
}
StringBuilder sb=new StringBuilder();
for (Integer integer : numbers) {
sb.append(integer);
}
return sb.toString();
}
}
真的没什么优化,但:
简单/残酷的方式将产生由位数字,如您存储它们,只要你得到一个数字你已经有了,你会生成一个新的随机数字。
一个更好的解决方案是最初存储可能的数字(比如说在一个列表中),并且对于每个数字,您将得到一个最大为列表大小的随机数(当列表从0开始减1)时,在此位置获取元素,并从列表中删除该元素。
实施例:
Possible digits : 123456789
picks a random element, let's say "3"
Possible digits : 12456789
等。
可能有很多方法可以解决这个问题,我在下面提供了两个。
第一招:
使用随机生成,从1-9到集中添加随机数字。
设置防止重复,所以继续发生,直到集合大小4.
第二个:
添加数字1-9到一个ArrayList,使用Collections.shuffle洗牌的数字。
取前4个数字。
注:不使用数字0,以防止0123变成123
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
public class QuickTester {
public static void main(String[] args) {
for(int i = 0; i < 3; i++) {
setRandom();
}
for(int i = 0; i < 3; i++) {
shuffleRandom();
}
}
/**
* Use a random generator, generate digits from 1-9,
* add them to the set (prevents duplicates) until set size is 4
*/
public static void setRandom() {
Random rand = new Random();
Set<Integer> set = new HashSet<Integer>();
while(set.size() < 4) {
set.add(rand.nextInt(9)+1);
}
String numStr = "";
for(Integer n : set) {
numStr += n;
}
int num = Integer.parseInt(numStr);
System.out.println(num);
}
/**
* Add digits 1-9 to an ArrayList, shuffle it using Collections.shuffle
* Take the first 4 digits
*/
public static void shuffleRandom() {
List<Integer> intList = new ArrayList<Integer>();
for(int i = 1; i < 10; i++) {
intList.add(i);
}
Collections.shuffle(intList);
String numStr = "";
for(int i = 0; i < 4; i++) {
numStr += intList.get(i);
}
int num = Integer.parseInt(numStr);
System.out.println(num);
}
}
输出:
3459
1359
2589
3456
2198
2153
所以你想选择4件事,然后剩下3件,然后剩下2件,现在拿走最后一件。关于如何:
n1 = generator.nextInt(4)+1;
n2 = generator.nextInt(3)+1;
if (!n2<n1) {n2 += 1;}//avoid the hole
n3 = generator.nextInt(2)+1;
if (!n3<n1) {n3 += 1;}//avoid the holes
if (!n3<n2) {n2 += 1;}
使用地图为您提供了更清晰的代码和更好的复杂性
public static void main(String[] args) {
Set<Integer> fourUniqueRandonNumbers = new HashSet<Integer>() ;
int maxItems = 4;
StringBuilder flatValueToRetun = new StringBuilder();
while (fourUniqueRandonNumbers.size()<maxItems){
int randomNumber = (int)(Math.random() * 9 + 1);
if(!fourUniqueRandonNumbers.contains(randomNumber)){
fourUniqueRandonNumbers.add(randomNumber);
flatValueToRetun.append(randomNumber);
}
}
}
你可以简单地实现这一点使用方法Collections.shuffle
。
List<Integer> l = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
Collections.shuffle(l);
Integer result = 1000*l.get(0) + 100*l.get(1) + 10*l.get(2) + l.get(3);
到目前为止你做了什么?你可以重复使用'math.random()',直到结果中没有重复的数字。 – likeitlikeit
习惯上,当问一个问题向我们展示你到目前为止尝试过的东西时。解决这个问题是算法制定中的一个简单练习。如果你试图自己想出一个方法,你会受益更多。 – scottb