Thursday, October 20, 2016

linear congruential generator, Pseudo random number

 A linear congruential generator (LCG) is an algorithm that yields a sequence of pseudo-randomized numbers calculated with a discontinuous piecewise linear equation. The method represents one of the oldest and best-known pseudorandom number generator algorithms 
https://en.wikipedia.org/wiki/Linear_congruential_generator 

Usage:





seed=rands(0,1,1)[0]; //ironic 
echo(Linear_Congruential_Generator(seed));

function  Linear_Congruential_Generator
   (seed=0,modulus=1e64, a=6364136223846793005, c=1442695040888963407)=
   (a * seed + c) % modulus;
 

Thursday, October 13, 2016

Run-length encoding (RLE)

Run-length encoding (RLE) is a very simple form of lossless data compression in which runs of data are stored as a single data value and count.

Encoder and decoder function showcase two kinds of conditional flow. Either type can be used for both jobs.
First case the values are prepared with conditions before a single call.
Second the condition is done first selecting between two calls of different values passes.

 Usage:



string1="hksdjfhkkkkdsfkljhriirenfcnlknd";
echo(RLEencode(string1));
string2="hhksdjfhkkkkdsfkljhriirenfcnlknd";
echo(RLEencode(string2));
string3=[0,1,1,2,2,2,2,1,1,0,-1,-1,-2,-2,-2,-2,-1,-1,0,1,1,2,2,2,2,1,1,0,-1,-1];
de=(RLEencode(string3));
echo(string3);
echo(de);
echo(RLEdecode(de));

function RLEencode(s,o,run=0,n=1)=
n==len(s)+1?o:
let(
newrun=s[n]==s[n-1]?run+1:0,
newo=s[n]==s[n-1]?o:concat(  o!=undef?o:[],[[run+1,s[ n-1]]])
)
RLEencode(s,newo,newrun,n+1)
;

function RLEdecode(s,o=[],run=1,n=-1)=
n==len(s)?o:
 let(next=[s[n][1]])
run ==1?

RLEdecode(s,concat(   o,next!=undef&&n>-1?next:[]),s[n+1][0],n+1)
:
RLEdecode(s,concat(   o,next!=undef?next:[] ),run-1,n);