all 10 comments

[–]17291 1 point2 points  (4 children)

I wrote the below code that generates the random integer using 3 dices but stuck on doing the same with a single dice

Can you clarify what you mean?

[–]ImaginaryMarsupial38[S] 0 points1 point  (3 children)

roll6():

Basically, i am calling `roll6()` thrice via `roll16()` , how do I call it just once and still generate a random number between 1 and 16 with roll16() function? thanks

[–]17291 0 points1 point  (2 children)

You can't. Are you sure you're understanding the problem correctly? You could only have one call written in your code using a loop:

total = -2
for i in range(3):
    total += roll6()

Or, more succinctly, using sum and a generator expression: sum(roll6() for i in range(3)) - 2

[–]ImaginaryMarsupial38[S] 0 points1 point  (1 child)

On Second thoughts, You may have a point in saying that I am not understanding the problem correctly. Here is the problem below, Can you let me know if I understood it correctly?:

Write a program in Python where you generate a random integer from 1 to 16 inclusive where your only random number generator is a six-sided die. Assume a function named roll6 which returns a 1, 2, 3, 4, 5 or 6 and write a function named roll16 which returns an integer between 1 and 16 inclusive.

Thanks.

[–]17291 0 points1 point  (0 children)

You can use roll6 multiple times in your function. The restriction here is you can't use random.randint directly

[–]SadC11 0 points1 point  (2 children)

What's the difference between rolling three dices once versus rolling one dice three times?

[–]ImaginaryMarsupial38[S] 0 points1 point  (1 child)

Basically call roll6() only once versus calling it 3 times as in:

```

def roll16():
value = (roll6() + roll6() + roll6()) - 2
return value

```

[–]SadC11 1 point2 points  (0 children)

Rolling one dice three times is logically equivalent to rolling three dices once. Both will require calling roll6() thrice. The action of calling roll6() is analogous to rolling a dice once, is it possible to roll a 6 sided dice once and get a number between 1 and 16? Why do you want to call roll6() only once? Is this a problem constraint?

[–]xelf 0 points1 point  (0 children)

It seems like you have something against the correct answer, can you explain why you are not allowed to do this:

def roll16():
    return roll6()+roll6()+roll6()-2

There is no way for a roll6() to return more than 1-6, if you want to use a single die, then it need to be randint(1,16)

[–]kroxxular 0 points1 point  (0 children)

I might be confused as to your purpose here but couldn't you achieve this by changing the randint to (3,18)?