Random Numbers in D Programming Language
Here’s the translation of the random numbers example from Go to D, formatted in Markdown suitable for Hugo:
import std.stdio;
import std.random;
void main()
{
// For example, uniform(0, 100) returns a random int n,
// 0 <= n < 100.
writef("%d,", uniform(0, 100));
writef("%d", uniform(0, 100));
writeln();
// uniform01() returns a float f,
// 0.0 <= f < 1.0.
writeln(uniform01());
// This can be used to generate random floats in
// other ranges, for example 5.0 <= f' < 10.0.
writef("%.15f,", uniform(5.0, 10.0));
writef("%.15f", uniform(5.0, 10.0));
writeln();
// If you want a known seed, create a new
// Random object with a specific seed.
auto rnd = Random(42);
writef("%d,", uniform(0, 100, rnd));
writef("%d", uniform(0, 100, rnd));
writeln();
// Using the same seed will produce the same sequence
// of random numbers.
auto rnd2 = Random(42);
writef("%d,", uniform(0, 100, rnd2));
writef("%d", uniform(0, 100, rnd2));
writeln();
}D’s std.random module provides pseudorandom number generation.
The uniform function is used to generate random integers within a specified range. For example, uniform(0, 100) returns a random int n, where 0 <= n < 100.
uniform01() returns a double f, where 0.0 <= f < 1.0.
This can be used to generate random floats in other ranges, for example 5.0 <= f' < 10.0, by using uniform(5.0, 10.0).
If you want a known seed, create a new Random object with a specific seed. This allows for reproducible sequences of random numbers.
Some of the generated numbers may be different when you run the sample.
$ rdmd random_numbers.d
68,56
0.808073179153353
5.840125017402497,6.937056298890035
94,49
94,49See the std.random module documentation for references on other random quantities that D can provide.