Pseudo-random numbers/Splitmix64: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 104: Line 104:


=={{header|Ada}}==
=={{header|Ada}}==
Ada conversion of Next_float * 5.0 to an integer value resulted in some values of 5 which would have caused a buffer overflow in the counting array. The fix was to truncate Next_float * 5.0 as a floating point value and convert that value to an integer. Upon doing this the counts of each array element become 20073, indicating there are an equal number values in each portion of the random number range.

The random number functions are written in a stand-alone package. The package is split into a package specification defining the interfaces to the public subprograms and a body containing the implementation of the random number generator.
The random number functions are written in a stand-alone package. The package is split into a package specification defining the interfaces to the public subprograms and a body containing the implementation of the random number generator.


Line 161: Line 159:
procedure Main is
procedure Main is
subtype idx is Integer range 0 .. 4;
subtype idx is Integer range 0 .. 4;
type answer_arr is array (idx) of Unsigned_64;
type answer_arr is array (idx) of Natural;
Vec : answer_arr := (others => 0);
Vec : answer_arr := (others => 0);
J : idx;
J : Integer;
fj : Float;
fj : Float;
begin
begin
Line 176: Line 174:
for I in 1 .. 100_000 loop
for I in 1 .. 100_000 loop
fj := Float'Truncation (next_float * 5.0);
fj := Float'Truncation (next_float * 5.0);
J := idx (fj);
J := Integer (fj);
Vec (J) := Vec (J) + 1;
Vec (J) := Vec (J) + 1;
end loop;
end loop;


for I in Vec'Range loop
for I in Vec'Range loop
Put_Line (I'Image & ":" & Unsigned_64'Image (Vec (J)));
Put_Line (I'Image & ":" & Integer'Image (Vec (I)));
end loop;
end loop;


end Main;</lang>
end Main;
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 192: Line 191:
4593380528125082431
4593380528125082431
16408922859458223821
16408922859458223821
0: 20073
0: 20027
1: 20073
1: 19892
2: 20073
2: 20073
3: 20073
3: 19978
4: 20073
4: 20030
</pre>
</pre>