/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-21 23:51:45 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20241221235145-4metak6z6u6vf6b0
[General] Major refactoring. Utils -> Vee nenaming.
Todo: Split libvee into a different library??

Also: Fixed spelling mistakes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 *
4
4
 * Created by Gustav Hartivgsson.
5
5
 */
6
 
[CCode (cname = "V", cprefix = "v_")]
7
 
namespace Utils {
 
6
 
 
7
namespace Vee {
8
8
  
9
9
  /**
10
10
   * At the moment this just wraps the GLib Rand things.
11
11
   * 
12
12
   * This also provides a static version of the methods.
13
13
   */
14
 
  [CCode (cname = "VRandom", cprefix = "v_random_")]
 
14
    
15
15
  public class Random {
16
16
    
17
 
    private static GLib.Once<Utils.Random> _instance;
 
17
    private static GLib.Once<Vee.Random> _instance;
18
18
    
19
19
    private GLib.Rand rand;
20
20
    
21
 
    [CCode (cname = "v_random_new")]
 
21
    
22
22
    private Random () {
23
23
      this.rand = new GLib.Rand ();
24
24
    }
25
25
    
26
 
    [CCode (cname = "v_random_get_instance")]
 
26
    
27
27
    public static Random get_instance () {
28
28
      return _instance.once (() => {
29
29
        return new Random ();
31
31
    }
32
32
    
33
33
    /* **** Instance versions *** */
34
 
    [CCode (cname = "v_random_get_double")]
 
34
    
35
35
    public double get_double () {
36
36
      return rand.next_double ();
37
37
    }
38
38
    
39
 
    [CCode (cname = "v_random_get_range")]
 
39
    
40
40
    public double get_double_range (double begin, double end) {
41
41
      return rand.double_range (begin, end);
42
42
    }
43
43
    
44
 
    [CCode (cname = "v_random_get_int")]
 
44
    
45
45
    public int32 get_int () {
46
46
      return (int32) rand.next_int ();
47
47
    }
48
48
    
49
 
    [CCode (cname = "v_random_get_int_range")]
 
49
    
50
50
    public int32 get_int_range (int32 begin, int32 end) {
51
51
      return rand.int_range ((int32) begin, (int32) end);
52
52
    }
53
53
    
54
 
    [CCode (cname = "v_random_seed")]
 
54
    
55
55
    public void seed (int32 seed) {
56
56
      rand.set_seed (seed.abs());
57
57
    }
58
58
    
59
59
    
60
60
    /* **** Static versions *** */
61
 
    [CCode (cname = "v_random_get_static_int")]
 
61
    
62
62
    public static int32 get_static_int () {
63
63
      Random r = Random.get_instance ();
64
64
      return r.get_int ();
65
65
    }
66
66
    
67
 
    [CCode (cname = "v_random_get_static_double")]
 
67
    
68
68
    public static double get_static_double () {
69
69
      Random r = Random.get_instance ();
70
70
      return r.get_double ();
71
71
    }
72
72
    
73
 
    [CCode (cname = "v_random_get_static_int_range")]
 
73
        
74
74
    public static int32 get_static_int_range (int32 begin, int32 end) {
75
75
      Random r = Random.get_instance ();
76
76
      return r.get_int_range (begin, end);
77
77
    }
78
78
    
79
 
    [CCode (cname = "v_random_get_static_double_range")]
 
79
    
80
80
    public static double get_static_double_range (double begin, double end) {
81
81
      Random r = Random.get_instance ();
82
82
      return r.get_double_range (begin, end);