How to resolve the algorithm Pseudo-random numbers/Xorshift star step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pseudo-random numbers/Xorshift star step by step in the Ada programming language

Table of Contents

Problem Statement

numbers as shown above. are as shown above

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pseudo-random numbers/Xorshift star step by step in the Ada programming language

Source code in the ada programming language

with Interfaces;  use Interfaces;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   const : constant Unsigned_64 := 16#2545_F491_4F6C_DD1D#;
   state : Unsigned_64          := 0;
   Unseeded_Error : exception;

   procedure seed (num : Unsigned_64) is
   begin
      state := num;
   end seed;

   function Next_Int return Unsigned_32 is
      x : Unsigned_64 := state;
   begin
      if state = 0 then
         raise Unseeded_Error;
      end if;

      x     := x xor (x / 2**12);
      x     := x xor (x * 2**25);
      x     := x xor (x / 2**27);
      state := x;
      return Unsigned_32 ((x * const) / 2**32);
   end Next_Int;

   function Next_Float return Long_Float is
   begin
      return Long_Float (Next_Int) / 2.0**32;
   end Next_Float;

   counts : array (0 .. 4) of Natural := (others => 0);
   J      : Natural;
begin
   seed (1_234_567);
   for I in 1 .. 5 loop
      Put_Line (Unsigned_32'Image (Next_Int));
   end loop;

   seed (987_654_321);
   for I in 1 .. 100_000 loop
      J          := Natural (Long_Float'Floor (Next_Float * 5.0));
      counts (J) := counts (J) + 1;
   end loop;

   New_Line;
   for I in counts'Range loop
      Put_Line (I'Image & " :" & counts (I)'Image);
   end loop;

end Main;


  

You may also check:How to resolve the algorithm Fibonacci sequence step by step in the PL/M programming language
You may also check:How to resolve the algorithm Isqrt (integer square root) of X step by step in the C programming language
You may also check:How to resolve the algorithm ABC problem step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Knapsack problem/Continuous step by step in the Erlang programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the SQL PL programming language