```
public class Solution{
Random r;
int[] nums;
public Solution(int[] nums){
Arrays.sort(nums);
this.nums = nums;
r = new Random();
}
public int pick(int target){
int lower = lowerBound(nums,target);
int upper = upperBound(nums,target);
if(nums[upper]==target) upper++;
return r.nextInt(upper-lower)+lower;
}
private int upperBound(int[] nums, int target){
int s = 0, e = nums.length-1;
while(s<e){
int m = s+(e-s)/2;
if(nums[m]>target) e = m;
else s = m+1;
}
return s;
}
private int lowerBound(int[] nums, int target){
int s = 0, e = nums.length-1;
while(s<e){
int m = s+(e-s)/2;
if(nums[m]>=target) e = m;
else s = m+1;
}
return s;
}
}
```