here's a set of numbers between [0,100].How to create a table of 10 arbitrary ranges to store them and pick appropriate intervals? My idea is to first create an array and cut 0~100 randomly. I'm stuck here.... How to create the intervals in a sequential order but also randomly?
Asked
Active
Viewed 67 times
2 Answers
2
Choose 9 distinct (without replacement) values between 0 and 100. Sort them (if you use the solution in the thread I linked, they'll already be sorted), and use them as your interval boundaries.
EDIT: If the ranges are allowed to be empty, don't even worry about uniqueness; just pick 9 random values, sort them, and you have your interval boundaries.
-
Thanks for your advice!!But what I mean is that I need the [0,100] to be cut into a sequential order such like [0,13],[14,45],[46,48]...some intervals like that.The boundaries I create must be in order. – user3298812 Feb 11 '14 at 20:27
-
@user3298812 Yes, my solution can give you that. Is there something I need to try to explain differently? – TypeIA Feb 11 '14 at 20:35
-
I saw that into more details and found the answer! Thanks a lot! You made my day ! – user3298812 Feb 11 '14 at 20:49
0
Here is Java implementation for this task:
import java.util.*;
public class Intervals {
public static int[][] getIntervals(int lowBound, int highBound,
int intervalCount) {
int intervalLength = highBound - lowBound;
boolean[] used = new boolean[intervalLength + 1];
used[0] = true;
used[intervalLength] = true;
Random r = new Random();
for (int i = 0; i < intervalCount - 1; i++) {
int candidate = 0;
while (used[candidate]) {
candidate = lowBound + r.nextInt(intervalLength);
}
used[candidate] = true;
}
int[][] result = new int[intervalCount][2];
int previous = -1;
int next;
int count = 0;
for (int i = 1; i <= intervalLength; i++) {
if (used[i]) {
next = i;
result[count][0] = previous + 1 + lowBound;
result[count][1] = next + lowBound;
count++;
previous = next;
}
}
return result;
}
public static void main(String[] args) {
System.out.println(Arrays.deepToString(getIntervals(0, 100, 10)));
}
}

Michael Egorov
- 21
- 1