我需要使用java
每天生成6位数的随机数不存储任何序列/对象状态。
目前我正在使用吹码。
//Locale is JAVA Locale Value like "IN","ZN","EN" etc.
public String generateTaceNumber(String locale) {
StringBuilder traceNumber = new StringBuilder(generateLocalTime(locale));
String traceNumberGenerated = traceNumber.toString();
List<Character> characters = new ArrayList<Character>();
for(char c:traceNumberGenerated.toCharArray()){
characters.add(c);
}
StringBuilder output = new StringBuilder(traceNumberGenerated.length());
while(characters.size()!=0){
//generate random number
int randPicker = (int)(Math.random()*characters.size());
output.append(characters.remove(randPicker));
}
//System.out.println("finaloutput :"+output);
return output.toString();
}
//it return lcoal time in string format.
public String generateLocalTime(String locale) {
Date yourDate = new Date();
Locale yourLocale = new Locale("en", LOCALE);
DateFormat tf = DateFormat.getTimeInstance(DateFormat.LONG, yourLocale);
String formattedTime = tf.format(yourDate);
//System.out.println("formattedTime "+formattedTime);
String time = formattedTime.substring(0, formattedTime.indexOf(' '));
String[] parts = time.split(":");
String formatedTimeVal = "";
for(int i=0; i < parts.length; i++) {
String subVal = parts[i];
if(subVal.length()==1) {
subVal = "0" + subVal;
}
formatedTimeVal = formatedTimeVal + subVal;
}
//System.out.println("formatted Time val :" +formatedTimeVal);
return formatedTimeVal;
}
输出:
formattedTime 8:09:54 PM IST
格式化时间值:080954
finaloutput:405809
以上代码给出6位数字,但问题是如果我在一秒内请求它给我相同的数字。
答案 0 :(得分:-1)
试试这个:
class randm{
long i = 0;
for(int j=0 ; j<10; j++)
{
i = (Math.random()*100000+100000);
System.out.println(i+" ");
}
}