我在Ibello和Java中进行过测试。我用
int random = (int)(Math.random() * (100000 - 1) + 1);
生成5位数代码。
我有一个测试类:
pthUgyintezesNewCase.I_Use_the_5digits();
allatiMellektermekSteps.Delete_the_old_datas();
pthNewCase.I_Save_And_Close(); //it brokes here
// Steps Class
public void I_Use_the_5digits() throws Exception{
int random = (int)(Math.random() * (100000 - 1) + 1);
//
如果应用程序生成比以往更多的5位数字,它就不会运行。是否有可能,如果它再次产生似乎,它会在一个数字上加1,直到它可以运行?
我想你想要生成一个5位数字,但有时候这个数字只有4位数?如果是这种情况,请尝试以下操作:
(int)(Math.random() * (100000 - 10000) + 10000);
通过使用10000而不是1,您可以确保该数字始终包含至少5位数
编辑:
这是一个检查下一个10'000'000数字的测试用例:
import java.lang.Math;
import java.util.Random;
public class HelloWorld
{
public static void main(String[] args)
{
for(int i = 0; i < 10000000; i++) {
int number = (int)(Math.random() * (100000 - 10000) + 10000);
if(String.valueOf(number).length() != 5) {
System.out.println(String.valueOf(number).length());
System.exit(0);
}
}
System.out.println("Finished with all 5 digit numbers");
}
}