Pseudo-random numbers/Combined recursive generator MRG32k3a: Difference between revisions

no edit summary
(→‎{{header|Perl}}: proper use of package/class)
No edit summary
Line 69:
* Show your output here, on this page.
 
 
=={{header|Ada}}==
<lang Ada>package MRG32KA is
type I64 is range -2**63..2**63 - 1;
m1 : constant I64 := 2**32 - 209;
m2 : constant I64 := 2**32 - 22853;
 
subtype state_value is I64 range 1..m1;
procedure Seed (seed_state : state_value);
function Next_Int return I64;
function Next_Float return Long_Float;
end MRG32KA;
</lang>
 
<lang Ada>
package body MRG32KA is
type Data_Array is array (0..2) of I64;
d : constant I64 := m1 + 1;
----------------
-- Generators --
----------------
a1 : Data_Array := (0, 1403580, -810728);
a2 : Data_Array := (527612, 0, -1370589);
x1 : Data_Array := (0, 0, 0);
x2 : Data_Array := (0, 0, 0);
----------
-- Seed --
----------
 
procedure Seed (seed_state : state_value) is
begin
x1 := (seed_state, 0, 0);
x2 := (seed_state, 0, 0);
end Seed;
 
--------------
-- Next_Int --
--------------
 
function Next_Int return I64 is
x1i : i64;
x2i : I64;
z : I64;
answer : I64;
begin
x1i := (a1(0) * x1(0) + a1(1) * x1(1) + a1(2) * x1(2)) mod m1;
x2i := (a2(0) * x2(0) + a2(1) * x2(1) + a2(2) * x2(2)) mod m2;
x1 := (x1i, x1(0), x1(1));
x2 := (x2i, x2(0), x2(1));
z := (x1i - x2i) mod m1;
answer := z + 1;
return answer;
end Next_Int;
 
----------------
-- Next_Float --
----------------
 
function Next_Float return Long_Float is
begin
return Long_float(Next_Int) / Long_Float(d);
end Next_Float;
 
end MRG32KA;
</lang>
 
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
with mrg32ka; use mrg32ka;
 
procedure Main is
counts : array(0..4) of Natural := (Others => 0);
J : Natural;
begin
seed(1234567);
for I in 1..5 loop
Put_Line(I64'Image(Next_Int));
end loop;
New_Line;
seed(987654321);
for I in 1..100_000 loop
J := Natural(Long_Float'Floor(Next_Float * 5.0));
Counts(J) := Counts(J) + 1;
end loop;
for I in Counts'Range loop
Put(I'Image & " :" & Counts(I)'Image);
end loop;
end Main;
</lang>
{{output}}
<pre>
1459213977
2827710106
4245671317
3877608661
2595287583
 
0 : 20002 1 : 20060 2 : 19948 3 : 20059 4 : 19931
</pre>
 
=={{header|Factor}}==
82

edits