0

I want to get how many attempts have been made before a random value between 0 and 10 is greater than 8. I have the following code which works correctly:

import numpy as np

x = np.random.uniform(low=0, high=10, size=1)
attempt = 1

while(x < 8):
    attempt = attempt + 1
    x = np.random.uniform(low=0, high=10, size=1)

However, now I want to get the number of attempts before x was greater than 8 for the fourth time. To do this, I placed the for loop just before the while loop which becomes like this:

for i in range(0,4):
    while(x < 8):
        attempt = attempt + 1
        x = np.random.uniform(low=0, high=10, size=1)

However, this is not working as I intended it to be. Can someone help me solve this problem?

2
  • 2
    this is not working as I intended it to be is hardly any useful information. What did you intend it to be? What did it actually do? Commented Feb 6, 2019 at 5:46
  • 1
    You never reset x so once x > 8 it stays that way for every interation of your for loop. Put x=0 between your for and your while. Commented Feb 6, 2019 at 5:47

5 Answers 5

2

You want to get the total number of attempts needed to obtain a random number 8 in 4 consecutive trails. Try this:

>>> import numpy as np
>>> def fun():
...     x = np.random.uniform(0,10,1)
...     attempt = 1
...     while x<8:
...             attempt += 1
...             x = np.random.uniform(0,10,1)
...     return attempt 
>>> for i in range(0,4):
...     print("Trial" , i , "took" , fun(), "Attempts")

Output:

Trial 0 took 1 Attempts
Trial 1 took 1 Attempts
Trial 2 took 8 Attempts
Trial 3 took 3 Attempts
Sign up to request clarification or add additional context in comments.

2 Comments

Can you also explain why @Beatrice's solution didn't work?
@DhruvJoshi, Beatrice's code works perfectly fine, but the only issue was that it wasn't generating the expected outcome.
0

The problem is that you're not resetting your x value. So once the variable is set to a value greater than 8, you code will not enter the while loop again. You need to set x = 0 before the while loop.

Comments

0

There are many ways of doing this. The below should work...

import numpy as np

successes = 0
rolls = 0
while (successes < 4):
    x = np.random.uniform(low=0, high=10, size=1)
    rolls += 1
    if x > 8:
        successes += 1

print(rolls)

Comments

0

This is not a for loop case. Try this:

while x < 8 and i <= 4:
    x = np.random.uniform(low=0, high=10, size=1)
    if x>8:
        i+=1
        x=np.random.uniform(low=0, high=10, size=1)
        attempt = 1
    print(attempt, x)
    attempt = attempt + 1

1 Comment

This won't work. OP needs 4 rolls above 8 not 4 rolls in total.
0

You should modify your code to

for i in range(0,4):
    x = np.random.uniform(low=0, high=10, size=1)
    while(x < 8):
        attempt = attempt + 1
        x = np.random.uniform(low=0, high=10, size=1)

This would reset x before it enters into the while loop. Without this statement, your the control enters into the while loop only once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.