我试图将时间分成相等的间隔,然后在一个间隔中选择一个随机时间。举个例子:分隔时间从上午8点到晚上8点,分为4(可以是2或3 ......)。我将得到8-11,11-2,2-5和5-8。然后在每个例子的间隔2中选择一个随机时间,并且有11:32:12。
我用
开始了我的功能private static List<Interval> splitDateTime(long start, long end, int intervalNo) {
long interval = (end - start) / intervalNo;
List<Interval> list = new ArrayList<Interval>();
for (long i = start + interval; i < end; i += interval) {
list.add(new Interval(start, i));
start=start + interval;
}
list.add(new Interval(start, end));
for (Interval t:list){
System.out.println(rant);
}
return list;
}
那我该怎么做呢?你有一些图书馆/代码/想法吗?乔达?
非常感谢提前!
答案 0 :(得分:0)
根据原始问题的评论,您在此处真正要求的是如何在两个间隔之间获得随机长值。这可以使用Random完成。
<div class="container">
<button>click me</button>
</div>
所以,如果你想在一个区间内得到一个值:
import java.util.Random
Random rand = new Random();
long val = rand.nextLong(50); //Returns a random value between 0 and 49
显然,用间隔的结束替换max,用间隔的开头替换min。
编辑:忘了提一下,这会给你一个值[min,max](不包括最大值)。如果你想要它是包容性的,只需在范围中加1:
long val = rand.nextLong([max]-[min]) + [min];
答案 1 :(得分:0)
没有任何库,您可以在从方法获取间隔列表并获取所需的库之后尝试此代码:
public long getRandomTimeFrom(Interval interval) {
Random r = new Random(); // random instatnce can be shared as class (static) field
return interval.getStart() + r.nextLong(interval.getEnd() - interval.getStart());
}
答案 2 :(得分:0)
这应该有效:
SecureRandom s = new SecureRandom();
public Interval randomInterval(long start, long end, int intervalNo) {
if(intervalNo == 0 || end <= start || (end-start)/intervalNo <= 1){
return null;
}
if(intervalNo == 1){
return new Interval(start, end);
}
System.err.println(start + " "+ end+" "+ intervalNo);
long interval = (end - start) / (intervalNo);
long value = Math.abs(s.nextInt(intervalNo));
long randomStart = start + (value*interval);
long randomEnd = start + ((value+1)*interval);
if(randomStart < start || randomStart > end || randomEnd <start || randomEnd > end || randomEnd < randomStart){
throw new RuntimeException("Start: "+start+" End: "+end+" RandomStart:"+randomStart+" RandomEnd:"+randomEnd);
}
return new Interval(randomStart, randomEnd);
}
@org.junit.Test
public void splitDateTime() {
for(int i=0;i<100000;i++){
int randomRange = Math.abs(s.nextInt())%100;
long start = System.currentTimeMillis();
long random = s.nextLong();
long end = start + (random %10000000+ System.currentTimeMillis())/10;
System.err.println(i+": "+randomInterval(start, end, randomRange));
}
}
答案 3 :(得分:0)
我把它改成了这样的东西,但它没有用。这个目的是为了: splitDateTime(8,20,4)=&gt; 11:32:12当前时间是11
SecureRandom s = new SecureRandom();
public Interval randomInterval(int startHour, int endHour, int intervalNo) {
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH), startHour, 0, 0);
long start = calendar.getTimeInMillis();
calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH), endHour, 0, 0);
long end = calendar.getTimeInMillis();
if(intervalNo == 0 || end <= start || (end-start)/intervalNo <= 1){
return null;
}
if(intervalNo == 1){
return new Interval(start, end);
}
long interval = (end - start) / (intervalNo);
long value = Math.abs(s.nextInt(intervalNo));
long randomStart = start + (value*interval);
long randomEnd = start + ((value+1)*interval);
if(randomStart < start || randomStart > end || randomEnd <start || randomEnd > end || randomEnd < randomStart){
throw new RuntimeException("Start: "+start+" End: "+end+" RandomStart:"+randomStart+" RandomEnd:"+randomEnd);
}
return new Interval(randomStart, randomEnd);
}
@org.junit.Test
public void splitDateTime() {
for(int i=0;i<24;i++){
for(int j=0;j<24;j++){
for(int k=0;k<24;k++){
if(randomInterval(i,j,k)!=null) {
System.err.println(i +" "+ j+" "+k);
System.err.println(randomInterval(i,j,k));
}
}
}
}
}
我更新了我的回答