tresses_0.1.0_ea960522/src/tresses-random.adb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--
--  SPDX-License-Identifier: MIT
--
--  Based on Braids from Mutable Instruments:
--  Copyright 2012 Emilie Gillet.

with Ada.Unchecked_Conversion;

package body Tresses.Random is

   function To_S16 is new Ada.Unchecked_Conversion (U16, S16);

   ----------
   -- Seed --
   ----------

   procedure Seed (This : in out Instance; Seed : U32) is
   begin
      This.State := Seed;
   end Seed;

   --------------
   -- Get_Word --
   --------------

   function Get_Word (This : in out Instance) return U32 is
   begin
      This.State := This.State * 1_664_525 + 1_013_904_223;
      return This.State;
   end Get_Word;

   ----------------
   -- Get_Sample --
   ----------------

   function Get_Sample (This : in out Instance) return S16 is
   begin
      return To_S16 (U16 (Shift_Right (Get_Word (This), 16)));
   end Get_Sample;

   ---------------
   -- Get_Float --
   ---------------

   function Get_Float (This : in out Instance) return Float is
   begin
      return Float (Get_Word (This)) / 4_294_967_296.0;
   end Get_Float;

end Tresses.Random;