JAVA
java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.stream.IntStream;
public class RandomWithoutRandom{
public static void main(String[]a) throws InterruptedException{
//find 10 random numbers between 2 and 20 (2 is included,20 is excluded):
for(int i=0;i<10;i++){
random(2,20,new String[a.length]);
}
}
static void random(int low,int high, String[]s){
// Each number divided by the given number (in this case "high" ) gives the remainder in the range [0- (high-1)]. To avoid result under lower limit, need to put filter (i>=low). The loop will iterating until the first number is reached under the required conditions (findFirst), and then the result will be presented.
IntStream.iterate(1,n->n+1). mapToLong(j-> System.nanoTime()/s.hashCode()%high).filter(i-> i>=low). findFirst().ifPresent(p -> System.out. println(p));
}
}
// The Array.hashCode method is inherited from Object, which means the hashcode depends on the reference (not on the content of array). Note: Arrays.hashCode depends on the content of array
// If we invoke Array.hashCode on same object, we will got same number each time. Because of that, we create new object each time(new String [] for each loop - line 6)...and we will got deferent number each time :)
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run