/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk

« back to all changes in this revision

Viewing changes to src/common/random.vala

  • Committer: Gustav Hartvigsson
  • Date: 2020-06-07 18:48:24 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20200607184824-jf14f7a1b1di2i2q
* Initial code - far from done

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * The contests of this file is in the Public Domain.
3
 
 *
4
 
 * Created by Gustav Hartivgsson.
5
 
 */
6
 
 
7
 
namespace Vee {
 
1
namespace VQDR.Common {
8
2
  
9
3
  /**
10
4
   * At the moment this just wraps the GLib Rand things.
11
5
   * 
12
6
   * This also provides a static version of the methods.
13
7
   */
14
 
    
15
8
  public class Random {
16
9
    
17
 
    private static GLib.Once<Vee.Random> _instance;
 
10
    private static Random? _instance = null;
18
11
    
19
12
    private GLib.Rand rand;
20
13
    
21
 
    
22
14
    private Random () {
23
15
      this.rand = new GLib.Rand ();
24
16
    }
25
17
    
26
 
    
27
18
    public static Random get_instance () {
28
 
      return _instance.once (() => {
29
 
        return new Random ();
30
 
      });
 
19
      if (_instance == null) {
 
20
       _instance = new Random ();
 
21
      }
 
22
      return _instance;
31
23
    }
32
24
    
33
25
    /* **** Instance versions *** */
34
 
    
35
26
    public double get_double () {
36
27
      return rand.next_double ();
37
28
    }
38
29
    
39
 
    
40
30
    public double get_double_range (double begin, double end) {
41
31
      return rand.double_range (begin, end);
42
32
    }
43
33
    
44
 
    
45
 
    public int32 get_int () {
46
 
      return (int32) rand.next_int ();
 
34
    public int get_int () {
 
35
      return (int) rand.next_int ();
47
36
    }
48
37
    
49
 
    
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);
52
40
    }
53
41
    
54
 
    
55
 
    public void seed (int32 seed) {
56
 
      rand.set_seed (seed.abs());
 
42
    public void seed (int seed) {
 
43
      rand.set_seed ((uint32) seed);
57
44
    }
58
45
    
59
46
    
60
47
    /* **** Static versions *** */
61
 
    
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 ();
65
51
    }
66
52
    
67
 
    
68
53
    public static double get_static_double () {
69
54
      Random r = Random.get_instance ();
70
55
      return r.get_double ();
71
56
    }
72
57
    
73
 
        
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);
77
61
    }
78
62
    
79
 
    
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);