1

I have to random number generators and I am trying to make a simple division test. I can't figure out how to make num2 and factor of num1 so I don't get a decimal as the answer.

I have tried to make a factor number generator that if num2 wasn't a factor of num1 it go back to the random number generator. here is the link https://replit.com/@Thom08/stuff4#main.py

import random
import replit
from csinsc import *
import math
def add(num1,num2):
  return num1 + num2
def subtract(num1,num2):
  return num1 - num2
def multiply(num1,num2):
  return num1 * num2
def divide(num1,num2):
  return num1 / num2
label .startstart
score = 0
amount = 0


label .start
if amount != 10:
  def multiply(num1,num2):
   return num1 * num2
  num1 = random.randint(1,10)
  num2 = random.randint(1,10)
  
  
  if num1 < num2:
     goto .start
  print("what is ", num1 ,'÷', num2,)
  p = int(input("ANSWER:  "))
  a = divide(num1,num2)
  if p == a:
    print("CORRECT")
    score = score + 1
    amount = amount + 1
    print("Your score is ", score , "/10")
    input("press enter to continue")
    replit.clear()
    goto .start
  else:
    print("INCORRECT")
  
    print("Your score is ", score , "/10")
    amount = amount + 1
    input("press ENTER to continue")
    replit.clear()
    goto .start

print("Congratulations, you finished the test")
print("Your score was ", score, "/10")
print("Would you like to do the test again")
answer = input("Type 'yes' or 'no':  ")
if answer == "yes":
  replit.clear()
  goto .startstart 
if answer == "no":
  print("BYE")
else:
  print("Could not compute")
  print("ID10T Error")
Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Thom08
  • 31
  • 3
  • 1
    What is your actual question? Do you want to _test_ that one number is a multiple of another, or do you want to _create_ a number that is guaranteed to be a multiple? – John Gordon May 04 '23 at 00:25
  • 6
    It's worth noting that goto support was added to Python as an [April Fools](http://entrian.com/goto/download.html) joke, and should probably not be used in new code. I would recommend learning how to use a `while` loop and the `break` statement instead. – Nick ODell May 04 '23 at 00:31

2 Answers2

1

You can generate a random integer between two other integers inclusive that is a multiple of some other integer, via the following code:

import random

def ceildiv(a, b):
    """From https://stackoverflow.com/a/17511341/16435355"""
    return -(a // -b)

def randint(a, b, m):
    """ Returns a random integer between a and b inclusive that is a multiple of m"""
    floor = ceildiv(a, m)
    ceil = b // m
    return random.randint(floor, ceil) * m

For example, the possible outputs of randint(1, 10, 3) are 3, 6 and 9.

You can determine if one integer is a multiple of another via the following code

def is_multiple(a, b):
  """ Test if a is a multiple of b"""
  return a%b == 0

For example, is_multiple(6,3) is True

Mous
  • 953
  • 3
  • 14
1

If you just need to randomly generate two numbers, one of which is a factor of another, then looping and checking if they're divisible is a bad idea. Instead, generate two numbers, then multiple them together. Both of the randomly generated numbers are guaranteed to be factors of the product, then you can use either of those numbers. For example:

import random


num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
prod = num1 * num2

print(f'{num1} is a factor of {prod}')
print(f'{num2} is a factor of {prod}')

However, this won't work if you need both to be under 10. In that case, you need to generate one number, find its factors, then randomly choose one of its factors using random.choice() to randomly get one of the factors.

Getting the factors of a number has already been addressed over here. To randomly get a factor, just use this:

import functools
import random


# Returns a random factor of n
def randFactor(n):
    factors = functools.reduce(list.__add__,
                               ([i, n//i] for i in range(1, int(n**0.5) + 1)
                                if n % i == 0))
    return random.choice(factors)


num1 = random.randint(1, 100)
num2 = randFactor(num1)
print(f'{num2} is a factor of {num1}')

The numbers generated are guaranteed to be factors of n on the first run, and it won't loop over and over again continuously checking.

Michael M.
  • 10,486
  • 9
  • 18
  • 34