about summary refs log tree commit diff
path: root/usth/ICT2.2/labwork/4/DiscreteDistro.java
blob: b603ed7b743a1a0e1fc81d279bcbc10e39aeea57 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;

import static java.util.Collections.binarySearch;
import static java.util.stream.Collectors.toList;

class DiscreteDistro
{
  public static void main(String... args)
  {
    var numbers = Stream.of(args).mapToInt(Integer::parseInt)
                                 .boxed().collect(toList());
    int n = numbers.size();
    for (int i = 1; i < n; ++i)
      numbers.set(i, numbers.get(i) + numbers.get(i - 1));

    int x = ThreadLocalRandom.current().nextInt(0, numbers.get(n - 1));
    int i = binarySearch(numbers, x) + 1;
    System.out.println(numbers);
    System.out.println(x);
    System.out.println((i < 0) ? -i : i);
  }
}