kristofer revised this gist . Go to revision
No changes
kristofer revised this gist . Go to revision
1 file changed, 1 insertion, 2 deletions
randomints.md
| @@ -25,7 +25,6 @@ min = 1 | |||
| 25 | 25 | max = 6 | |
| 26 | 26 | ||
| 27 | 27 | def randomInt(min, max): | |
| 28 | - | return random.randint(min, max) | |
| 29 | - | ||
| 28 | + | return random.randint(min, max) | |
| 30 | 29 | # will return random ints from 1 to 6 | |
| 31 | 30 | ``` | |
kristofer revised this gist . Go to revision
1 file changed, 1 deletion
randomints.md
| @@ -27,6 +27,5 @@ max = 6 | |||
| 27 | 27 | def randomInt(min, max): | |
| 28 | 28 | return random.randint(min, max) | |
| 29 | 29 | ||
| 30 | - | ||
| 31 | 30 | # will return random ints from 1 to 6 | |
| 32 | 31 | ``` | |
kristofer revised this gist . Go to revision
1 file changed, 5 insertions, 1 deletion
randomints.md
| @@ -6,7 +6,8 @@ If you need to generate a random integer, a number from say, 1 to 6, you can wri | |||
| 6 | 6 | import java.util.Random; | |
| 7 | 7 | ||
| 8 | 8 | int min = 1; | |
| 9 | - | int max = 6; | |
| 9 | + | int max = 6; | |
| 10 | + | ||
| 10 | 11 | Random random = new Random(); | |
| 11 | 12 | ||
| 12 | 13 | public int randomInt(min, max) { | |
| @@ -20,6 +21,9 @@ public int randomInt(min, max) { | |||
| 20 | 21 | ```python | |
| 21 | 22 | import random | |
| 22 | 23 | ||
| 24 | + | min = 1 | |
| 25 | + | max = 6 | |
| 26 | + | ||
| 23 | 27 | def randomInt(min, max): | |
| 24 | 28 | return random.randint(min, max) | |
| 25 | 29 | ||
kristofer revised this gist . Go to revision
1 file changed, 28 insertions
randomints.md(file created)
| @@ -0,0 +1,28 @@ | |||
| 1 | + | If you need to generate a random integer, a number from say, 1 to 6, you can write a couple routines: | |
| 2 | + | ||
| 3 | + | ### Java | |
| 4 | + | ||
| 5 | + | ```java | |
| 6 | + | import java.util.Random; | |
| 7 | + | ||
| 8 | + | int min = 1; | |
| 9 | + | int max = 6; | |
| 10 | + | Random random = new Random(); | |
| 11 | + | ||
| 12 | + | public int randomInt(min, max) { | |
| 13 | + | return random.nextInt((max + 1) - min + 1) + min; | |
| 14 | + | } | |
| 15 | + | // will return random ints from 1 to 6 | |
| 16 | + | ``` | |
| 17 | + | ||
| 18 | + | ### Python | |
| 19 | + | ||
| 20 | + | ```python | |
| 21 | + | import random | |
| 22 | + | ||
| 23 | + | def randomInt(min, max): | |
| 24 | + | return random.randint(min, max) | |
| 25 | + | ||
| 26 | + | ||
| 27 | + | # will return random ints from 1 to 6 | |
| 28 | + | ``` | |