Skip to main content


Showing posts from January, 2015

First post using stackedit

Welcome to StackEdit! Hey! I’m your first Markdown document in StackEdit1. Don’t delete me, I’m very helpful! I can be recovered anyway in the Utils tab of the Settings dialog.
Documents StackEdit stores your documents in your browser, which means all your documents are automatically saved locally and are accessible offline!
StackEdit is accessible offline after the application has been loaded for the first time.Your local documents are not shared between different browsers or computers.Clearing your browser’s data may delete all your local documents! Make sure your documents are synchronized with Google Drive or Dropbox (check out the Synchronization section). Create a document The document panel is accessible using the button in the navigation bar. You can create a new document by clicking New document in the document panel.
Switch to another document All your local documents are listed in the document panel. You can switch from one to another by clicking a document in the li…

Generating a random number between 1 and 6 using only a function which generate 0 or 1

Suppose you have a function toss, which returns 0 or 1 randomly.

Now you need to write a function which uses toss () to generate a random integer number between 1 and 6.

The idea is: you toss 3 times, and if the value is outside of first six values, you toss 3 times again.

The Python code is following:

#suppose we have a function called "toss" which return 0 and 1 randomly #we need to write a function to generate number between 1 and 6 #means 1 <= x <= 6 use only the function "toss" from random import randint def toss (): return randint (0, 1) def generateRandomBetween1And6 (): while (True): sum = 0 for i in range (3): sum += toss () * (2 ** i) if (sum >= 1 and sum <= 6): return sum #test res = [0] * 6 for i in range (10000): num = generateRandomBetween1And6 (); res [num - 1] += 1 for i in range (6): print(str(res[i]) + '\n')