#! /usr/bin/env python3 # -*- coding: utf-8; mode: python -*- """ Tiny Python 2/3 script to output a random number, like if your friend was telling it: $ EtLaUnMecTeDit__NIST Hmmm, 1DC97AAEA5B7D694C0376111702D34A15279066D15E45A8FDC3292835FEA4AA20D7E5EB097700380CE700EC625D60043E9F1D9319AB778EA4D464B68F7D7AD4E $ EtLaUnMecTeDit__NIST Hmmm, 14F1EB1615F65CD2AB0A7F0302250DE55D8F42F835A6A0B9A2B0F4292745826B27389A64771434CC94F769D73592AE08AAD09C382E0F10D931CE2A9D28DA2125 You can reduce the number of digits: $ EtLaUnMecTeDit__NIST 10 Hmmm, 14F1EB1615 $ EtLaUnMecTeDit__NIST 12 Hmmm, 14F1EB1615F6 It uses the NIST beacon (https://beacon.nist.gov/home) to output "true" random generator. - *Date:* 14/06/2017 - *Author:* Lilian Besson, © 2017. - *Licence:* MIT Licence (http://lbesson.mit-license.org). """ from __future__ import print_function # Python 2 compatibility if needed from sys import argv try: from urllib.request import urlretrieve except ImportError: from urllib import urlretrieve from tempfile import NamedTemporaryFile import xml.etree.ElementTree as ET URL = "https://beacon.nist.gov/rest/record/last" def randint_from_beacon(maxlen=None, beacon_url=URL): """Read 'https://beacon.nist.gov/rest/record/last' and output a random number.""" with NamedTemporaryFile() as this_temp_file: path_of_f = this_temp_file.name urlretrieve(beacon_url, path_of_f) tree = ET.parse(path_of_f) root = tree.getroot() tag = root.tag result = root.find(tag.replace('}record', '}outputValue')).text if maxlen is not None: result = result[:maxlen] return result if __name__ == '__main__': maxlen = int(argv[1]) if len(argv) > 1 else None print("Hmmm,", randint_from_beacon(maxlen))