Pseudo-random numbers/Xorshift star: Difference between revisions
Content added Content deleted
(→{{header|REXX}}: added the computer programming language REXX.) |
m (→{{header|REXX}}: changed output alignment, added/changed comments and whitespace, optimized the NEXT function.) |
||
Line 488: | Line 488: | ||
if seed2=='' | seed2=="," then seed2= 987654321 /* " " " " " " */ |
if seed2=='' | seed2=="," then seed2= 987654321 /* " " " " " " */ |
||
const= x2d(2545f4914f6cdd1d) /*initialize the constant to be used. */ |
const= x2d(2545f4914f6cdd1d) /*initialize the constant to be used. */ |
||
o.12= copies(0, 12) /*construct 12 bits of zeros. */ |
|||
o.25= copies(0, 25) /* " 25 " " " */ |
|||
o.27= copies(0, 27) /* " 27 " " " */ |
|||
w= max(3, length(n) ) /*for aligning the left side of output.*/ |
w= max(3, length(n) ) /*for aligning the left side of output.*/ |
||
state= seed1 /* " the state to seed #1. */ |
state= seed1 /* " the state to seed #1. */ |
||
do j=1 for n |
do j=1 for n |
||
if j==1 then do; say center('n', w) |
if j==1 then do; say center('n', w) " pseudo─random number" |
||
say copies('═', w) |
say copies('═', w) " ══════════════════════════" |
||
end |
end |
||
say right(j':', w)" " right(commas(next()), |
say right(j':', w)" " right(commas(next()), 18) /*display a random number*/ |
||
end /*j*/ |
end /*j*/ |
||
say |
say |
||
Line 512: | Line 515: | ||
exit 0 /*stick a fork in it, we're all done. */ |
exit 0 /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
b2d: parse arg ?; return x2d( b2x(? |
b2d: parse arg ?; return x2d( b2x(?) ) /*convert bin ──► decimal. */ |
||
d2b: parse arg ?; return right(x2b(d2x(? |
d2b: parse arg ?; return right( x2b( d2x(?) ), 64, 0) /*convert dec ──► 64 bit bin.*/ |
||
commas: parse arg _; do ?=length(_)-3 to 1 by -3; _= insert(',', _, ?); end; return _ |
commas: parse arg _; do ?=length(_)-3 to 1 by -3; _= insert(',', _, ?); end; return _ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
next: procedure expose state const; |
next: procedure expose state const o.; x= d2b(state) /*convert STATE to binary. */ |
||
x = xor(x, left( |
x = xor(x, left( o.12 || x, 64) ) /*shift right 12 bits and XOR*/ |
||
x = xor(x, right( x || |
x = xor(x, right( x || o.25, 64) ) /* " left 25 " " " */ |
||
x = xor(x, left( |
x = xor(x, left( o.27 || x, 64) ) /* " right 27 " " " */ |
||
state= b2d(x) /*set STATE to the latest X.*/ |
state= b2d(x) /*set STATE to the latest X.*/ |
||
return b2d( left( d2b(state * const), 32) ) /*return a |
return b2d( left( d2b(state * const), 32) ) /*return a pseudo─random num.*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
xor: parse arg a, b; |
xor: parse arg a, b; $= /*perform a bit─wise XOR. */ |
||
do !=1 for length(a); $= $ || (substr(a,!,1) && substr(b,!,1) ) |
do !=1 for length(a); $= $ || (substr(a,!,1) && substr(b,!,1) ) |
||
end /*!*/; return $</lang> |
|||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
n pseudo─random number |
n pseudo─random number |
||
═══ ══════════════════════════ |
|||
═══ ══════════════════════ |
|||
1: 3,540,625,527 |
1: 3,540,625,527 |
||
2: 2,750,739,987 |
2: 2,750,739,987 |
||
3: 4,037,983,143 |
3: 4,037,983,143 |
||
4: 1,993,361,440 |
4: 1,993,361,440 |
||
5: 3,809,424,708 |
5: 3,809,424,708 |
||
# count of pseudo─random # |
# count of pseudo─random # |