Home > front end >  Python RNG and counter
Python RNG and counter

Time:12-28

I'm having trouble on a homework assignment that is asking me to do as follows. Write a code that conduct following actions:

  1. Define a function that returns an integer between 1 and 10.

  2. Call the defined function to get the random integer.

  3. Continue drawing a random integer until you get a number that is larger than or equal to 8.

  4. Display how many draws did it take before you stopped

You should import random module.

I welcome the completion of this assignment but would be very grateful for an explanation as well. I'm currently at a loss of how to keep track of how many draws it took before I stopped. What I have so far is as follows, please help?

from random import randint
rng= randint(1,11)
def random_number_generator():
    print(rng)
    while rng<=7: 
        break
random_number_generator()

CodePudding user response:

from random import randint  
rng = randint(1, 11)  # get a random number between 1 and 11

def random_number_generator():  # define a function
    print(rng)  # print random generated number
    while rng <= 7:  # if rng <= 7 condition is True
        break  # break out of while loop immediately
    # no return statement, so random_number_generator returns none

random_number_generator()  # Call function

Going by the code you provided, lets look at the 4 tasks you need to solve.

  1. Define a function that returns an integer between 1 and 10.

As you already know, you can define a function with the keyword def. So your function header def random_number_generator() defines a function with the name random_number_generator that takes no arguments when the function is called, hence the empty parenthesis ().

In Python every function returns None per default. If you want to change this behaviour, you have to add a return statement to your function.

def get_two():
    return 2

x = get_two()  # store return value in variable x
print(x)  # prints 2

get_two returns the number 2 when called.

  1. Call the defined function to get the random integer.

You correctly imported randint from the random module to generate a random integer between 1 and 10. But you have to be careful with your choice of numbers as randint has both values inclusive. That is randint(1, 11) will give you a random integer between 1 and 11.

  1. Continue drawing a random integer until you get a number that is larger than or equal to 8.

Continue drawing in this context means to call the random_number_generator function over and over again until you get a number that is larger than or equal to 8.

Your idea of the while loop with the given condition was correct. But think about the placement. Does it make sense to have the loop inside a function that only generates 1 random number per call?

  1. Display how many draws did it take before you stopped

Use the print function to display how many draws you did. In order to get the number of draws you can add a counter variable, that gets incremented with each loop. With each loop you add 1 to your counter variable. There are 2 options for you to do this:

  1. counter = counter 1 which can be a handful
  2. That's why there is a shorthand for that counter = 1 which is the same as 1. but you don't need to write counter twice.

With this you can built a simple counter just like this:

counter = 0
i = 10
while i > 3:
    counter  = 1  # Increment counter by 1 each loop
    i -= 2  # Decrement i by 2 each loop
print(counter)  # prints 4

With that being said you can now start to implement the given tasks.

  1. Import randint from random module
  2. Define a function that returns a random number between 1 and `10``
  3. Repeat generating a random number until it is greater than or equal to 8
  4. Count how many draws it took until loop terminated.
from random import randint  # 1. 

def random_number_generator():  # 2.
    return randint(1, 10)

counter = 0  # Initialize counter with 0

while random_number_generator() < 8:  # Draw a number until number is >= 8
    counter  = 1  # increment counter to count draws
print(counter, "draw(s) needed until a number greater than or equal 8 was drawn")  # Display draws needed

As you don't need to store the randomly generated number, you can use the function call of random_number_generator in your loop condition to check if the number is greater than or equal 8.

Inside the loop you'll just count how many times you need to draw a number.

But you are free to store the value in a variable rng to check against the loop condition. In this case you need to assign random_number_generator() twice to rng. One time outside the while loop to initialize the variable rng for the loop condition and a second time inside the loop to change the value rng is holding.

CodePudding user response:

See comments in code below.

'''
Generate random integers until a value equal or greater than 8 is returned.
Report how many attempts it took.
''' 
from random import randint

# Define a function which ONLY returns a random integer between 1 and 10 

def rand_int():
    return (randint(1,10)) # parameters are inclusive, i.e. can return both 1 and 10

# Set initial values for a counter and the random integer result

counter = 0
random = 0

while random <= 7: # Continue running while random int is too low
    counter  = 1 #Increment counter variable by 1 each loop
    random = rand_int()
    print('Current count is ', counter, ', integer is ', random)
    if counter > 100:
        print('Too many tries, something has probably gone wrong')
        break

# Display success message

print('Success! Integer ', random, ' returned after ', counter, 'attempts!')
  • Related