Random number of a Random number

04-25-2020 05:43 PM
New Contributor

Ok, let me see if I can get this down for every one to understand...

I am doing a guessing game in python/PyQt5...

I am trying to put in a hint button. So I am trying to generate a random number of a random number, but I don't know what function to use.

so the random number is generated and if you hit the hint button I want the second random number to be within +/-  (N) of the initial random number

import random
import sys

from PyQt5.QtWidgets import *

from frmguess import Ui_frmguess

# Declare global variables to store random number and times guessed
times_guessed = 0
num_to_guess = random.randrange(1, 101)                             #Gives random number
random2 = random.randrange(5, 5, num_to_guess)                  ????what function to use????

# Create main window (inherits from the base QT widget)
class Mainwindow(QWidget):
     def __init__(self):
          """Initialization code for when mainwindow object is created"""
          # Call UI set up in QtDesignerpyuic
          self.ui = Ui_frmguess()
          # Connect guess button click signal to guess_click slot function
          # Reset times guessed label
          # Connect hint button click signal to hint_button
          # Show the form
     def hint_click(self):
          """What to do when user clicks the hint button"""
          # Get access to random number generated
          global num_to_guess
          global random2
          # Tell user is plus or minus another random number
          hint, okPressed = QMessageBox.question(self, "Your hint is...",
          "Number is plus or minus five " + str(random2),

Any leads would be great

Tags (1)
0 Kudos
4 Replies
MVP Esteemed Contributor

You are using the right method, just not using it correctly:

random.randrange(num_to_guess-5, num_to_guess+5)
0 Kudos
New Contributor

Ok, I tried that way, the results were not what I wanted. Sorry I didn't explain further, every time I user hits the 'hint' button I wanted that random number to be random also. Let's say my first random number to guess (R1) is 31. I want the hint button to be +/- 5 of 31 but to be random also every time button is hit (R2).

Joshua, your way just gives the same number over and over for R2. 

I did fumble through it late last night thanks to pizza and beer. I have put the R2 code at the beginning and in the loop of the hint button and it randomizes every time it is hit but stays within 5 of R1 and the code looks like this

random2 = num_to_guess + random.randrange(-5, 6)

Thank you Joshua for your reply, it was much appreciated. I realized this morning I didn't totally explain my desire correctly.

0 Kudos
MVP Esteemed Contributor

Something is off then if the R2 is returning the same value every time.  The following shows that calling the second function multiple times generates different numbers each time:

>>> import random
>>> num_to_guess = random.randrange(1, 101)
>>> num_to_guess
>>> random.randrange(num_to_guess-5, num_to_guess+5)
>>> for i in range(6):
...     random.randrange(num_to_guess-5, num_to_guess+5)
0 Kudos
New Contributor

I believe the biggest problem I had was the R2 was not in my loop, but it is fixed now. Thank you

0 Kudos