简体   繁体   中英

How to generate a random number that is evenly divisible by another randomly generated number

I'm working on a simple math flashcards app using JavaScript and jQuery. The available operations are addition, subtraction, multiplication, and division, each of which have functions that use generateTop() and generateBottom() to assign values to HTML elements.

I'm having trouble figuring out how to use my division problem function to generate random numbers so that the topNumber is evenly divisible by the bottomNumber . I'm using the following code to generate random numbers for each problem:

let topNumber = 0;
let bottomNumber = 0;

function generateTop(max, min){
  topNumber = Math.floor(Math.random() * (max - min) + min);
  return topNumber;
};

function generateBottom(max, min){
  bottomNumber = Math.floor(Math.random() * (max - min) + min);
  return bottomNumber;
};

Here is my current division problem generator:

function division(){
  problemTop.html(generateTop(1, 144));
  problemBottom.html(generateBottom(1, topNumber));
  opSym.html("÷");
}

I can adjust the max and min to get a positive integer for addition, subtraction, and multiplication problems but am stuck on how to ensure that I get a positive integer for division problems. I've tried messing with some different loop ideas but haven't gotten anything to work. I want to keep min at 1 and max at 144.

I've checked SO for solutions but can only find how to generate random numbers divisible by one other explicit, hard-coded number. How can I adjust my division function so that the topNumber can be divided evenly by the bottomNumber ?

Add a denomenator parameter to your generateTop function, and pass in the randomly generated "bottom" (denominator) number returned from generateBottom . Multiply the new random number created in generateTop by the denominator, and return that.

You can do like this :

 var a = Math.floor(Math.random() * 10) var b = a * Math.floor(Math.random() * 10) var c = b/a; document.write(b + " is divisible by " + a + " ( *" + c + ")" )

In a * b, b is always divisible by a.

 let topNumber = 0; let bottomNumber = 0; function generateTop(max, min, a){ topNumber = a * Math.floor(Math.random() * (max - min) + min); return topNumber + " is divisible by " + a + " ( *" + topNumber/a + ")"; }; function generateBottom(max, min){ bottomNumber = Math.floor(Math.random() * (max - min) + min); return bottomNumber; }; document.write(generateTop(10, 1, generateBottom(10, 1)))

Just add a single if statement to make your function recursive:

function generateBottom(max, min) {
    bottomNumber = Math.floor(Math.random() * (max - min) + min);
    if (topNumber % bottomNumber) {
        generateBottom(max, min);
    }
    return bottomNumber;
}

What this does is it divides topNumber and checks if a remainder exists. If there is a remainder, it means the two do not divide, so it is recalculated. Otherwise, it returns bottomNumber as per usual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM