Last active
December 16, 2015 04:28
-
-
Save libswan/5376875 to your computer and use it in GitHub Desktop.
L13 Problem 7
This file contains hidden or 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
| """ | |
| This code will produce "The output of random.randint(1, 10) after a specific seed is shown below." | |
| """ | |
| import random | |
| random.seed(9001) | |
| print random.randint(1, 10) | |
| print random.randint(1, 10) | |
| print random.randint(1, 10) | |
| print random.randint(1, 10) | |
| print random.randint(1, 10) | |
| """Output | |
| 1 | |
| 3 | |
| 6 | |
| 6 | |
| 7 | |
| """ | |
| """ | |
| Explanation: @asterism (Adaptation) | |
| a) All of the problems (below) use random.randint(1, 10), just like above. | |
| b) They also use the same seed, 9001 - random.seed(9001) - just like above. | |
| c) Therefore, when you see random.seed(9001) and random.randint(1, 10) (below) you know | |
| it will return the SAME numbers as above, in the SAME sequence: | |
| 1, 3, 6, 6, 7. | |
| """ | |
| random.seed(9001) | |
| for i in xrange(random.randint(1, 10)): | |
| print random.randint(1, 10) | |
| """ | |
| Explanation: @Alfredo42 (Adaptation) | |
| a) The for loop calls random.randint for the 1st time. | |
| a.i) Which sets the for loop to read: for i in xrange(1) (the 1st number in the "pre-est. random sequence" = 1, 3, 6, 6, 7). | |
| a.ii) Which sets the for loop to run: 1 time. | |
| b) The print statement, calls random.randint for a 2nd time. | |
| b.i) Therefore, the print statement prints the 2nd number in the sequence, which is: 3. | |
| c) Notes: | |
| c.i) I would like to understand why we used xrange here and not range? | |
| c.ii) Is my a.i) explanation accurate? Could it be improved? | |
| """ | |
| random.seed(9001) | |
| d = random.randint(1, 10) | |
| for i in xrange(random.randint(1, 10)): | |
| print d | |
| """ | |
| Explanation: @Alfredo42 (Adaptation) | |
| a) The "d =" statement calls random.randint for the 1st time. | |
| a.i) Which sets d to be the 1st number in the sequence, which is: 1. | |
| b) The for loop calls random.randint for the 2nd time. | |
| b.i) Which sets the for loop to read: for i in xrange(3) (the 2nd number in the "pre-est. random sequence" = 1, 3, 6, 6, 7). | |
| b.ii) Which sets the for loop to run: 3 times. | |
| c) The print statement, calls "print d", which = 1. | |
| c.i) Given the for loop runs 3 times (see b.ii), 1 is printed 3 times. | |
| """ | |
| random.seed(9001) | |
| d = random.randint(1, 10) | |
| for i in xrange(random.randint(1, 10)): | |
| if random.randint(1, 10) < 7: | |
| print d | |
| else: | |
| random.seed(9001) | |
| d = random.randint(1, 10) | |
| print random.randint(1, 10) | |
| """ | |
| Explanation: @Alfredo42 (Adaptation) | |
| a) The "d =" statement calls random.randint for the 1st time. | |
| a.i) Which sets d to be the 1st number in the sequence, which is: 1. | |
| b) The for loop calls random.randint for the 2nd time. | |
| b.i) Which sets the for loop to read: for i in xrange(3) (the 2nd number in the "pre-est. random sequence" = 1, 3, 6, 6, 7). | |
| b.ii) Which sets the for loop to run: 3 times. | |
| c) 1st run of if statement: The if statement calls random.randint for the 3rd time. | |
| c.i) Which sets "random.randint(1, 10)" = 6. | |
| c.ii) As 6 is < 7, it prints d, which = 1. | |
| d) 2nd run of if statement: The if statement calls random.randint for the 4th time. | |
| d.i) Which sets "random.randint(1, 10)" = 6. | |
| d.ii) As 6 is < 7, it prints d, which = 1. | |
| e) 3rd run of if statement: The if statement calls random.randint for the 5th time. | |
| e.i) Which sets "random.randint(1, 10)" = 7. | |
| e.ii) As 7 is NOT < 7, it goes to the else statement, which calls on random.randint 2 times... | |
| e.iii) Which therefore prints the 2nd number in the "pre-est. random sequence = 3. | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment