Random Numbers in Mercury
Here’s the translation of the Go code to Java, with explanations in Markdown format suitable for Hugo:
Java’s java.util.Random
class provides pseudorandom number generation.
import java.util.Random;
public class RandomNumbers {
public static void main(String[] args) {
Random rand = new Random();
// For example, nextInt(100) returns a random int n,
// 0 <= n < 100.
System.out.print(rand.nextInt(100) + ",");
System.out.println(rand.nextInt(100));
// nextDouble() returns a double f,
// 0.0 <= f < 1.0.
System.out.println(rand.nextDouble());
// This can be used to generate random doubles in
// other ranges, for example 5.0 <= f' < 10.0.
System.out.print((rand.nextDouble() * 5) + 5 + ",");
System.out.println((rand.nextDouble() * 5) + 5);
// If you want a known seed, create a new
// Random object with a specific seed.
Random r2 = new Random(42);
System.out.print(r2.nextInt(100) + ",");
System.out.println(r2.nextInt(100));
Random r3 = new Random(42);
System.out.print(r3.nextInt(100) + ",");
System.out.println(r3.nextInt(100));
}
}
Some of the generated numbers may be different when you run the sample.
$ javac RandomNumbers.java
$ java RandomNumbers
68,56
0.8090228139659177
5.840125017402497,6.937056298890035
94,49
94,49
See the java.util.Random class documentation for references on other random quantities that Java can provide.
Note: Java doesn’t have an exact equivalent to Go’s math/rand/v2
package with PCG (Permuted Congruential Generator). The standard java.util.Random
class uses a different algorithm. For more advanced random number generation, you might want to look into the java.util.concurrent.ThreadLocalRandom
class or third-party libraries that implement PCG.
Markdown format suitable for Hugo:
Java's `java.util.Random` class provides pseudorandom number generation.
```java
import java.util.Random;
public class RandomNumbers {
public static void main(String[] args) {
Random rand = new Random();
// For example, nextInt(100) returns a random int n,
// 0 <= n < 100.
System.out.print(rand.nextInt(100) + ",");
System.out.println(rand.nextInt(100));
// nextDouble() returns a double f,
// 0.0 <= f < 1.0.
System.out.println(rand.nextDouble());
// This can be used to generate random doubles in
// other ranges, for example 5.0 <= f' < 10.0.
System.out.print((rand.nextDouble() * 5) + 5 + ",");
System.out.println((rand.nextDouble() * 5) + 5);
// If you want a known seed, create a new
// Random object with a specific seed.
Random r2 = new Random(42);
System.out.print(r2.nextInt(100) + ",");
System.out.println(r2.nextInt(100));
Random r3 = new Random(42);
System.out.print(r3.nextInt(100) + ",");
System.out.println(r3.nextInt(100));
}
}
Some of the generated numbers may be different when you run the sample.
$ javac RandomNumbers.java
$ java RandomNumbers
68,56
0.8090228139659177
5.840125017402497,6.937056298890035
94,49
94,49
See the java.util.Random class documentation for references on other random quantities that Java can provide.
Note: Java doesn’t have an exact equivalent to Go’s math/rand/v2
package with PCG (Permuted Congruential Generator). The standard java.util.Random
class uses a different algorithm. For more advanced random number generation, you might want to look into the java.util.concurrent.ThreadLocalRandom
class or third-party libraries that implement PCG.