2
* The contests of this file is in the Public Domain.
4
* Created by Gustav Hartivgsson.
1
namespace VQDR.Common {
10
4
* At the moment this just wraps the GLib Rand things.
12
6
* This also provides a static version of the methods.
15
8
public class Random {
17
private static GLib.Once<Vee.Random> _instance;
10
private static Random? _instance = null;
19
12
private GLib.Rand rand;
22
14
private Random () {
23
15
this.rand = new GLib.Rand ();
27
18
public static Random get_instance () {
28
return _instance.once (() => {
19
if (_instance == null) {
20
_instance = new Random ();
33
25
/* **** Instance versions *** */
35
26
public double get_double () {
36
27
return rand.next_double ();
40
30
public double get_double_range (double begin, double end) {
41
31
return rand.double_range (begin, end);
45
public int32 get_int () {
46
return (int32) rand.next_int ();
34
public int get_int () {
35
return (int) rand.next_int ();
50
public int32 get_int_range (int32 begin, int32 end) {
38
public int get_int_range (int begin, int end) {
51
39
return rand.int_range ((int32) begin, (int32) end);
55
public void seed (int32 seed) {
56
rand.set_seed (seed.abs());
42
public void seed (int seed) {
43
rand.set_seed ((uint32) seed);
60
47
/* **** Static versions *** */
62
public static int32 get_static_int () {
48
public static int get_static_int () {
63
49
Random r = Random.get_instance ();
64
50
return r.get_int ();
68
53
public static double get_static_double () {
69
54
Random r = Random.get_instance ();
70
55
return r.get_double ();
74
public static int32 get_static_int_range (int32 begin, int32 end) {
58
public static int get_static_int_range (int begin, int end) {
75
59
Random r = Random.get_instance ();
76
60
return r.get_int_range (begin, end);
80
63
public static double get_static_double_range (double begin, double end) {
81
64
Random r = Random.get_instance ();
82
65
return r.get_double_range (begin, end);