
Mark Six is a lottery in Hong Kong. You can select 6 numbers from a pool of 49 numbers on the lottery ticket. There are numerous single-line commands that can be used to generate 6 numbers randomly from 1 to 49.
python 2
python -c 'import random; print [ random.randint(1,49) for _ in xrange(6)]'
awk
awk -v min=1 -v max=49 'BEGIN{ srand(); for (i = 0; i < 6; i++) print int(min+rand()*(max-min+1))}'
bash
for i in {1..6}; do echo $(( ( RANDOM % 49 ) + 1)); done
jot on Mac macOS 10.13 High Sierra no longer provides jot
jot -r -s ' ' 6 1 49 | tr ' ' '\n'
However, you will soon find repeated numbers are generated from the above solutions. The trick to have non-repeated generated numbers is using random sort from an array with 49 numbers,
python 2
python -c 'import random; a = range(1, 49); random.shuffle(a); print a[:6:]'
jot on Mac macOS 10.13 High Sierra no longer provides jot
jot 49 1 49 | gsort --random-sort | head -n 6
Using AI to write python script
Using AI to generate Python scripts is extremely straightforward. Let’s write a prompt and compare it with what we wrote in 2017. The prompt is: Write a Python script that pick six random unique numbers from 1 to 49, inclusive. Write it simple and testable.
import random
def get_random_numbers():
return random.sample(range(1, 50), 6)
print(get_random_numbers())
The result is great! We can add AI to unit test as well.
import unittest
from your_script import get_random_numbers
class TestGetRandomNumbers(unittest.TestCase):
def test_length(self):
self.assertEqual(len(get_random_numbers()), 6)
def test_values(self):
numbers = get_random_numbers()
self.assertTrue(all(1 <= x <= 49 for x in numbers))
def test_uniqueness(self):
numbers = get_random_numbers()
self.assertTrue(len(set(numbers)) == len(numbers))
if __name__ == '__main__':
unittest.main()
The unit test is better than many developers!
Comments
Please accept the "Functionality" cookie category to view and post comments.
Comments failed to load. You can try again or view the discussion directly on GitHub.
View on GitHub