export const chooseKRandomElements = < T > ( arr , k ) : Array < T > => { const reservoir = [ ] ; for ( let i = 0 ; i < k ; i ++ ) { reservoir . push ( arr [ i ] ) ; } for ( let i = k ; i < arr . length ; i ++ ) { const j = Math . round ( Math . random ( ) * i ) ; if ( j < k ) { reservoir [ j ] = arr [ i ] ; } } return reservoir ; } ;