using subroutines to generate the random was too expensive for this performance-intensive procedure. reverted to built-ins
removed comments. this is not planned; inefficient performance.
add index on deviceid