Wednesday, December 25, 2013

java.util.Random in Java 8

One of the neat features of java.util.Random class in Java 8 is that it has been retrofitted to now return a random Stream of numbers.


For eg, to generate an infinite stream of random doubles between 0(inclusive) and 1(exclusive):

Random random = new Random();
DoubleStream doubleStream = random.doubles();

or to generate an infinite stream of integers between 0(inclusive) and 100(exclusive):

Random random = new Random();
IntStream intStream = random.ints(0, 100);


So what can this infinite random stream be used for, I will demonstrate a few scenarios, do keep in mind though that since this is an infinite stream, any terminal operations has to be done once the stream has been made limited in some way, otherwise the operation will not terminate!

For eg. to get a stream of 10 random integers and print them:
intStream.limit(10).forEach(System.out::println);

Or to generate a list of 100 random integers :

List<Integer> randomBetween0And99 = intStream
                                       .limit(100)
                                       .boxed()
                                       .collect(Collectors.toList());


For gaussian pseudo-random values, there is no stream equivalent of random.doubles(), however it is easy to come up with one with the facility that Java 8 provides:

Random random = new Random();
DoubleStream gaussianStream = Stream.generate(random::nextGaussian).mapToDouble(e -> e);

Here, I am using the Stream.generate api call passing in a Supplier which generates the next gaussian with the already available method in Random class.


So now to do something a little more interesting with the stream of pseudo-random doubles and the stream of Gaussian pseudo-random doubles, what I want to do is to get the distribution of doubles for each of these two streams, the expectation is that the distribution when plotted should be uniformly distributed for pseudo-random doubles and should be normal distributed for Gaussian pseudo-random doubles.

In the following code, I am generating such a distribution for a million pseudo-random values, this uses a lot of facilities provided by the new Java 8 Streams API:

Random random = new Random();
DoubleStream doubleStream = random.doubles(-1.0, 1.0);
LinkedHashMap<Range, Integer> rangeCountMap = doubleStream.limit(1000000)
        .boxed()
        .map(Ranges::of)
        .collect(Ranges::emptyRangeCountMap, (m, e) -> m.put(e, m.get(e) + 1), Ranges::mergeRangeCountMaps);

rangeCountMap.forEach((k, v) -> System.out.println(k.from() + "\t" + v));

and this code spits out data along these lines:
-1	49730
-0.9	49931
-0.8	50057
-0.7	50060
-0.6	49963
-0.5	50159
-0.4	49921
-0.3	49962
-0.2	50231
-0.1	49658
0	50177
0.1	49861
0.2	49947
0.3	50157
0.4	50414
0.5	50006
0.6	50038
0.7	49962
0.8	50071
0.9	49695

and similarly generating a distribution for a million Gaussian pseudo-random values:
Random random = new Random();
DoubleStream gaussianStream = Stream.generate(random::nextGaussian).mapToDouble(e -> e);
LinkedHashMap<Range, Integer> gaussianRangeCountMap =
        gaussianStream
                .filter(e -> (e >= -1.0 && e < 1.0))
                .limit(1000000)
                .boxed()
                .map(Ranges::of)
                .collect(Ranges::emptyRangeCountMap, (m, e) -> m.put(e, m.get(e) + 1), Ranges::mergeRangeCountMaps);

gaussianRangeCountMap.forEach((k, v) -> System.out.println(k.from() + "\t" + v));

Plotting the data gives the expected result:

For a pseudo-random Data:


And for Gaussian Data:


Complete code is available in a gist here - https://gist.github.com/bijukunjummen/8129250

Monday, December 23, 2013

Java 8 parameter name at runtime

Java 8 will be introducing an easier way to discover the parameter names of methods and constructors.

Prior to Java 8, the way to find the parameter names is by turning the debug symbols on at the compilation stage which adds meta information about the parameter names in the generated class files then to extract the information which is complicated and requires manipulating the byte code to get to the parameter names.

With Java 8, though the compilation step with debug symbols on is still required to get the parameter names into the class byte code, the extraction of this information is way simpler and is supported with Java reflection, for eg. Consider a simple class:

public class Bot {
    private final String name;
    private final String author;
    private final int rating;
    private final int score;

    public Bot(String name, String author, int rating, int score) {
        this.name = name;
        this.author = author;
        this.rating = rating;
        this.score = score;
    }
    
    ...
}    

theoretically, a code along these lines should get hold of the parameter names of the constructor above:

Class<Bot> clazz = Bot.class;
Constructor ctor = clazz.getConstructor(String.class, String.class, int.class, int.class);
Parameter[] ctorParameters =ctor.getParameters();

for (Parameter param: ctorParameters) {
    System.out.println(param.isNamePresent() + ":" + param.getName());
}

Parameter is a new reflection type which encapsulates this information. In my tests with Java Developer Preview (b120), I couldn't get this to work though!


Update: Based on a suggestion in the comments, the way to get the parameter name information into the class byte code is using a "-parameters" compiler argument, for eg this way through maven-compiler plugin:
<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>3.1</version>
 <configuration>
  <source>1.8</source>
  <target>1.8</target>
        <compilerArgument>-parameters</compilerArgument>
 </configuration>
</plugin>

References:

http://openjdk.java.net/jeps/118