Unittest for generateRandom

Adds a unittest for generateRandom and modify the generateRandom function so that it passes the test. Additionally it is cleaner and shorter now.