import random def rand2(): return 1 if random.random() > 0.5 else 0 def rand3(): rand2_binary = ''.join([rand2(), rand2()]) rand2_int = int(rand2_binary, 2) if rand2_int > 2: return rand3() return rand2_int samples = [rand2() for i in range(0, 100)] print('0: {}/100'.format(str(samples.count(0)))) print('1: {}/100'.format(str(samples.count(1)))) print('2: {}/100'.format(str(samples.count(2))))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question