Created
November 3, 2013 23:39
-
-
Save mounirmesselmeni/7296072 to your computer and use it in GitHub Desktop.
Create random number between 1 and 7 based on a random function that return a number between 1 and 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import random | |
def rand5(): | |
return random.randint(1,5) | |
def rand7(): | |
result = 0 | |
while True: | |
result = 5 * (rand5() - 1) + rand5() | |
if result <= 21: | |
break | |
#result is now a random between 1 and 25 | |
return (result%7) + 1 | |
class RandomTest(unittest.TestCase): | |
def test_random7(self): | |
for index in range(100000): | |
random_number = rand7() | |
self.assertLessEqual(random_number, 7) | |
self.assertGreaterEqual(random_number, 1) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment