random.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-1.6.0).
03  * Copyright (c) 2007-2014 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
19  */
20 package org.jenetics.internal.math;
21 
22 import org.jenetics.util.StaticObject;
23 
24 /**
25  * Some random helper functions.
26  *
27  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
28  @since 1.4
29  @version 1.4 &mdash; <em>$Date: 2014-02-15 $</em>
30  */
31 public final class random extends StaticObject {
32     private random() {}
33 
34     /*
35      * Conversion methods used by the 'Random' engine from the JDK.
36      */
37 
38     public static float toFloat(final int a) {
39         return (a >>> 8)/((float)(<< 24));
40     }
41 
42     public static float toFloat(final long a) {
43         return (int)(a >>> 40)/((float)(<< 24));
44     }
45 
46     public static double toDouble(final long a) {
47         return (((a >>> 38<< 27(((int)a>>> 5))/(double)(1L << 53);
48     }
49 
50     public static double toDouble(final int a, final int b) {
51         return (((long)(a >>> 6<< 27(b >>> 5))/(double)(1L << 53);
52     }
53 
54     /*
55      * Conversion methods used by the Apache Commons BitStreamGenerator.
56      */
57 
58     public static float toFloat2(final int a) {
59         return (a >>> 9)*0x1.0p-23f;
60     }
61 
62     public static float toFloat2(final long a) {
63         return (int)(a >>> 41)*0x1.0p-23f;
64     }
65 
66     public static double toDouble2(final long a) {
67         return (a & 0xFFFFFFFFFFFFFL)*0x1.0p-52d;
68     }
69 
70     public static double toDouble2(final int a, final int b) {
71         return (((long)(a >>> 6<< 26(b >>> 6))*0x1.0p-52d;
72     }
73 
74 }