ip696 :
I create a random stream
Random random = new Random();
Stream<Integer> boxed = random.ints(0, 100000000).boxed();
But I need 60% of the numbers generated to be 0, while the remaining can be truly random. How can I do it?
EDIT:
And I need only positive numbers and between 0-100
1
2
0
0
9
0
0
1
12
dasblinkenlight :
Since the size of the target interval is divisible by ten, you can count on the last digit of generated numbers being uniformly distributed. Hence, this simple approach should work:
- Generate numbers in the range 0..1000
- If the last digit of the random number
r
is 0..5, inclusive, return zero - Otherwise, return
r / 10
Here is this approach in code:
Stream<Integer> boxed = random.ints(0, 1000).map(r -> r%10 < 6 ? 0 : r/10).boxed();
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=436864&siteId=1