Random Numbers in Fortress
Based on the provided input, here’s the translation of the Go code to Java, along with the explanation in Markdown format suitable for Hugo:
Our first example demonstrates how to generate random numbers in Java. Here’s the full source code:
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 rand2 = new Random(42);
System.out.print(rand2.nextInt(100) + ",");
System.out.println(rand2.nextInt(100));
Random rand3 = new Random(42);
System.out.print(rand3.nextInt(100) + ",");
System.out.println(rand3.nextInt(100));
}
}
To run the program, compile and execute it using the javac
and java
commands:
$ javac RandomNumbers.java
$ java RandomNumbers
68,56
0.8090228139659177
5.840125017402497,6.937056298890035
94,49
94,49
Some of the generated numbers may be different when you run the sample.
Java’s java.util.Random
class provides pseudorandom number generation. The Random
class uses a 48-bit seed, which is modified using a linear congruential formula.
If you need more control over the random number generation process or require better statistical properties, you might want to look into the java.util.concurrent.ThreadLocalRandom
class for multi-threaded applications or the java.security.SecureRandom
class for cryptographically strong random numbers.
See the Java API documentation for references on other random quantities that Java can provide.