/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/libvee/random.vala

  • Committer: Gustav Hartvigsson
  • Date: 2024-12-22 00:42:11 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20241222004211-eg5i6yqp677vmc2n
[libvee/fast_number.vala] Removed debug code.

Show diffs side-by-side

added added

removed removed

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