user
Some definitions to help in the explanation

:   Floor operation  

    :   https://en.wikipedia.org/wiki/Floor\_and\_ceiling\_functions
    :   Greatest integer less than or equal to a real number.

:   Bitwise Logical shift operators (c-inspired)  

    :   https://en.wikipedia.org/wiki/Bitwise\_operation#Bit\_shifts
    :   Binary bits of value shifted left or right, with zero bits shifted in where appropriate.
    :   Examples are shown for 8 bit binary numbers; most significant bit to the left.

:   :   **<<** Logical shift left by given number of bits.

        :   E.g Binary 00110101 **<<** 2 == Binary 11010100

:   :   **>>** Logical shift right by given number of bits.

        :   E.g Binary 00110101 **>>** 2 == Binary 00001101

:   **^** Bitwise exclusive-or operator

    :   https://en.wikipedia.org/wiki/Exclusive\_or
    :   Bitwise comparison for if bits differ

        :   E.g Binary 00110101 **^** Binary 00110011 == Binary 00000110

Xorshift\_star Generator (pseudo-code)

```
   /* Let u64 denote an unsigned 64 bit integer type. */
   /* Let u32 denote an unsigned 32 bit integer type. */
   
   class Xorshift_star
       u64 state       /* Must be seeded to non-zero initial value */
       u64 const = HEX '2545F4914F6CDD1D'
   
       method seed(u64 num):
           state =  num
       end method
       
       method next_int():
           u64 x = state
           x = x ^ (x >> 12)
           x = x ^ (x << 25)
           x = x ^ (x >> 27)
           state = x
           u32 answer = ((x * const) >> 32)
           
           return answer
       end method
       
       method next_float():
           return float next_int() / (1 << 32)
       end method
       
   end class
       

```

:   Xorshift use

```
   random_gen = instance Xorshift_star
   random_gen.seed(1234567)
   print(random_gen.next_int())   /* 3540625527 */
   print(random_gen.next_int())   /* 2750739987 */
   print(random_gen.next_int())   /* 4037983143 */
   print(random_gen.next_int())   /* 1993361440 */
   print(random_gen.next_int())   /* 3809424708 */

```

Task

* Generate a class/set of functions that generates pseudo-random

numbers as shown above.

* Show that the first five integers genrated with the seed 1234567

are as shown above

* Show that for an initial seed of 987654321, the counts of 100\_000 repetitions of

```
   floor(random_gen.next_float() * 5)

```

:   Is as follows:

```
   0: 20103, 1: 19922, 2: 19937, 3: 20031, 4: 20007

```

* Show your output here, on this page.
assistant
**Works with**: Rakudo version 2020.07

**Translation of**: Python

Raku does not have unsigned Integers at this time (Integers are arbitrary sized) so use explicit bit masks during bitwise operations. All constants are encapsulated inside the class.

```
class Xorshift-star {
    has $!state;

    submethod BUILD ( Int :$seed where * > 0 = 1 ) { $!state = $seed }

    method next-int {
        $!state +^= $!state +> 12;
        $!state +^= $!state +< 25 +& (2⁶⁴ - 1);
        $!state +^= $!state +> 27;
        ($!state * 0x2545F4914F6CDD1D) +> 32 +& (2³² - 1)
    }

    method next-rat { self.next-int / 2³² }
}

# Test next-int
say 'Seed: 1234567; first five Int values';
my $rng = Xorshift-star.new( :seed(1234567) );
.say for $rng.next-int xx 5;


# Test next-rat (since these are rational numbers by default)
say "\nSeed: 987654321; first 1e5 Rat values histogram";
$rng = Xorshift-star.new( :seed(987654321) );
say ( ($rng.next-rat * 5).floor xx 100_000 ).Bag;


# Test with default seed
say "\nSeed: default; first five Int values";
$rng = Xorshift-star.new;
.say for $rng.next-int xx 5;

```

Output:

```
Seed: 1234567; first five Int values
3540625527
2750739987
4037983143
1993361440
3809424708

Seed: 987654321; first 1e5 Rat values histogram
Bag(0(20103), 1(19922), 2(19937), 3(20031), 4(20007))

Seed: default; first five Int values
1206177355
2882512552
3117485455
1303648416
241277360
```