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