I fixed the random function plus helper function

The random function isn't giving out contract violation anymore. And I
manage to shorten beatlookup function into like 4 lines.