1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.rng.core.source64;
19
20 import org.apache.commons.rng.core.util.NumberFactory;
21 import org.apache.commons.rng.core.BaseProvider;
22
23
24
25
26
27 public abstract class LongProvider
28 extends BaseProvider
29 implements RandomLongSource {
30
31
32 @Override
33 public long nextLong() {
34 return next();
35 }
36
37
38 @Override
39 public int nextInt() {
40 return NumberFactory.makeInt(nextLong());
41 }
42
43
44 @Override
45 public double nextDouble() {
46 return NumberFactory.makeDouble(nextLong());
47 }
48
49
50 @Override
51 public boolean nextBoolean() {
52 return NumberFactory.makeBoolean(nextLong());
53 }
54
55
56 @Override
57 public float nextFloat() {
58 return NumberFactory.makeFloat(nextInt());
59 }
60
61
62 @Override
63 public void nextBytes(byte[] bytes) {
64 nextBytesFill(this, bytes, 0, bytes.length);
65 }
66
67
68 @Override
69 public void nextBytes(byte[] bytes,
70 int start,
71 int len) {
72 checkIndex(0, bytes.length - 1, start);
73 checkIndex(0, bytes.length - start, len);
74
75 nextBytesFill(this, bytes, start, len);
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 static void nextBytesFill(RandomLongSource source,
93 byte[] bytes,
94 int start,
95 int len) {
96 int index = start;
97
98
99
100 final int indexLoopLimit = index + (len & 0x7ffffff8);
101
102
103 while (index < indexLoopLimit) {
104 final long random = source.next();
105 bytes[index++] = (byte) random;
106 bytes[index++] = (byte) (random >>> 8);
107 bytes[index++] = (byte) (random >>> 16);
108 bytes[index++] = (byte) (random >>> 24);
109 bytes[index++] = (byte) (random >>> 32);
110 bytes[index++] = (byte) (random >>> 40);
111 bytes[index++] = (byte) (random >>> 48);
112 bytes[index++] = (byte) (random >>> 56);
113 }
114
115 final int indexLimit = start + len;
116
117
118 if (index < indexLimit) {
119 long random = source.next();
120 while (true) {
121 bytes[index++] = (byte) random;
122 if (index < indexLimit) {
123 random >>>= 8;
124 } else {
125 break;
126 }
127 }
128 }
129 }
130 }