JetBrains Academy: Random - Kamil-Jankowski/Learning-JAVA GitHub Wiki
JetBrains Academy: Random
Generate random numbers:
You are given three numbers: a
, b
and n
.
Your task is to output the sum of the first n
random numbers in a range from a
to b
inclusively. The seed of the generator should be set as a + b
.
The input contains numbers in a single line in the following order: n
, a
, b
.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a = scanner.nextInt();
int b = scanner.nextInt();
int seed = a + b;
int sum = 0;
Random random = new Random(seed);
for (int i = 0; i < n; i++) {
sum += random.nextInt(b - a + 1) + a;
}
System.out.println(sum);
}
}
Find the seed:
Your job is to find the seed between A and B (both inclusively) that produces N pseudorandom numbers from 0 (inclusive) to K (exclusive). It should also have the maximum of these N numbers to be the minimum among all maximums of other seeds in this range.
Sounds complicated? Take a look at the example.
Here we have A = 7, B = 9, N = 4, K = 100. Let's suppose:
- For the seed 7, we get sequence 45, 99, 23, 67 – the maximum is 99.
- For the seed 8, we get 64, 34, 23, 9 – the maximum is 64.
- For the seed 9, we get 78, 34, 0, 11 – the maximum is 78.
Then the minimum among these maximums is 64. That means, in this example, the seed we are looking for is 8.
The input contains numbers A, B, N, K in a single line in this order.
Your task is to output 2 numbers: a seed and it's maximum. If there are some seeds with equal minimal maximums, you should output the seed that is less than all other seeds.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int n = scanner.nextInt();
int k = scanner.nextInt();
int minimumOfMax = Integer.MAX_VALUE;
int currentMinSeed = 0;
int randomNumber;
for (int i = a; i <= b; i++){
Random random = new Random(i);
int currentMax = 0;
for (int j = 0; j < n; j++){
randomNumber = random.nextInt(k);
if (randomNumber > currentMax){
currentMax = randomNumber;
}
}
if (currentMax < minimumOfMax){
minimumOfMax = currentMax;
currentMinSeed = i;
}
}
System.out.println(currentMinSeed);
System.out.println(minimumOfMax);
}
}
Gaussian random numbers:
Find the first seed that is greater or equal to K where each of N Gaussian numbers is lesser than or equal M.
The input contains numbers K, N, M in a single line.
You should output the seed.
import java.util.*;
public class Main {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
int n = scanner.nextInt();
double m = scanner.nextDouble();
int seed = k-1;
boolean seedFound = false;
while (!seedFound){
seed++;
Random random = new Random(seed);
for (int i = 0; i < n; i++) {
if (random.nextGaussian() <= m){
seedFound = true;
} else {
seedFound = false;
break;
}
}
}
System.out.println(seed);
}
}