我如何使 2 个随机 int 生成器不生成相同的整数?假设程序运行,我的示例类如下。感谢您的帮助。
public class TestRandom{
int num;
public TestRandom(){
num = 0;
}
public TestRandom(int x){
num = x;
}
public String toString(){
String print;
if (num == 1)
print = "1";
else if(num == 2)
print = "2";
else
print = "---";
return print;
}
}
public class Test{
TestRandom[] s= new TestRandom[20];
Random ran = new Random();
public void setArray(){
for(int i=0; i<s.length; i++)
s[i] = new TestRandom();
for(int k=0; k<4; k++)
int RandomNum = ran.nextInt(s.length);
s[RandomNum] = new TestRandom(1);
for(int e=0; e<4; e++)
int RandomNum = ran.nextInt(s.length);
s.[RandomNum] = new TestRandom(2);
}
}