Xorshift coded in Powershell


This is xorshift function coded in PowerShell v2.
XORSHIFT is known as fast and secure ramdom number generation function. I implemanted bit shift logic (logical bit shift) by handwriting, because the bit shift operations -shl and -shr are not supportted in PowerShell v2. These functions consist of simple multiplication and division.
function leftshift([uint32]$v, [uint32]$s){
    for($i = 0; $i -lt $s; $i++){
        if($v -gt 0x7fffffff){
            $v += 0x80000000
        }
        $v *= 2
    }
    return $v
}
function rightshift([uint32]$v, [uint32]$s){
    for($i = 0; $i -lt $s; $i++){
        $v = [math]::floor($v/2)
    }
    return $v
}

I also implemented string version.
function leftshift2([uint32]$v, [uint32]$s){
    $bit = ([convert]::ToString($v,2) + "".padright($s,"0")).padleft(32,"0")
    return [convert]::ToUint32($bit.substring($bit.length - 32, 32),2)
}
function rightshift2([uint32]$v, [uint32]$s){
    $bit = "".padright($s,"0") + ([convert]::ToString($v,2).padleft(32,"0"))
    return [convert]::ToUint32($bit.substring(0, 32),2)
}

I compared which method is computationally efficient.
#Multiplication and Division Version
PS C:\Users\user> C:\Users\user\Desktop\xorshift.ps1
1st gen : 3701687786
2nd gen : 458299110
3rd gen : 2500872618
4th gen : 3633119408
4th gen : 516391518
5th gen : 2377269574
total : 0.3742156

#String Version
PS C:\Users\user> C:\Users\user\Desktop\xorshift.ps1
1st gen : 3701687786
2nd gen : 458299110
3rd gen : 2500872618
4th gen : 3633119408
4th gen : 516391518
5th gen : 2377269574
total : 0.2812385

In theory, the method to generate following bit shift x86_assembry when converting to machine code should be fastest.
shl eax, 3
 or
shr eax, 3

So method using -shl and -shr are faster than others (BUT it is available Powershell version 3.0 or higher).
PS C:\Users\user> C:\Users\user\Desktop\xorshift.ps1
1st gen : 3701687786
2nd gen : 458299110
3rd gen : 2500872618
4th gen : 3633119408
4th gen : 516391518
5th gen : 2377269574
total : 0.0630053

Anyway, this is 128bit xorshift function.

Xorshift.ps1
$DebugPreference = "silentlycontinue"
#$DebugPreference = "continue"
#$DebugPreference = "inquire"
#$DebugPreference = "stop"
#write-debug ""

[uint32]$script:x = 123456789
[uint32]$script:y = 362436069
[uint32]$script:z = 521288629
[uint32]$script:w = 88675123

function leftshift([uint32]$v, [uint32]$s){
    for($i = 0; $i -lt $s; $i++){
        if($v -gt 0x7fffffff){
            $v += 0x80000000
        }
        $v *= 2
    }
    return $v
}
function leftshift2([uint32]$v, [uint32]$s){
    $bit = ([convert]::ToString($v,2) + "".padright($s,"0")).padleft(32,"0")
    return [convert]::ToUint32($bit.substring($bit.length - 32, 32),2)
}
function rightshift([uint32]$v, [uint32]$s){
    for($i = 0; $i -lt $s; $i++){
        $v = [math]::floor($v/2)
    }
    return $v
}
function rightshift2([uint32]$v, [uint32]$s){
    $bit = "".padright($s,"0") + ([convert]::ToString($v,2).padleft(32,"0"))
    return [convert]::ToUint32($bit.substring(0, 32),2)
}
function xorshift(){
    [uint32]$t = $script:x -bxor (leftshift $script:x 11)
    $script:x = $script:y
    $script:y = $script:z
    $script:z = $script:w
    return $script:w = (($script:w -bxor (rightshift $script:w 19)) -bxor ($t -bxor (rhightshift $t 8)))
    #return $script:w
}
echo ("1st gen : " + (xorshift).ToString())
echo ("2nd gen : " + (xorshift).ToString())
echo ("3rd gen : " + (xorshift).ToString())
echo ("4th gen : " + (xorshift).ToString())
echo ("4th gen : " + (xorshift).ToString())
echo ("5th gen : " + (xorshift).ToString())
$proctime = measure-command {
    for($i=0;$i-lt1000;$i++){
        xorshift
    }
}
echo ("total : " + $proctime.TotalSeconds.ToString())
Profile
I have technical job experience in enbedded software development and server side infrastructure/application engineering. I'm interested in programming and computer security.
Objective
To write down my technical knowledge in the place where I can access from anywhere. To share my program source code. To train my writing skill.
Link
  • LinkedIn (preparing)

  • Twitter

  • Facebook (preparing)

  • GitHub

  • StackOverFlow (preparing)

Archives