In the sparse implementation, randomization is over instances in the training set, in the dense version, randomization is over batches. The loops collect the different rows of Y into a dense matrix. 

I believe it should be enough to randomize batches, but I did not want to break your functionality.