## 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`

?

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.

**Programming - Random Numbers,** Other properties, such as no (predictable) relation between the current random Generating a sequence of random numbers is quite difficult because we want certain When choosing a numbers between 0 and 1000 we are equally likely to get any number. Matlabs random number generation function is called rand. Click on a worksheet cell where you want the random number to reside. Type the function =RAND () into the formula bar above the worksheet. Press the F9 key to change the RAND function into a static random number. Press the Enter key on the keyboard to display the random number into the selected cell.

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.

**Generating random numbers in ProcessingJS (article),** Learn how to generate random numbers in ProcessingJS using the random() function, and check their Typically we want to generate random numbers that are picked relatively evenly. You buy a bag of M&Ms to share with other students. Say the "another number" is K, and you want a random number X between 0 and N (inclusive) that is divisible by K. Then choose a random number Y between 0 and N/K and multiply it by K. #include <random> std::default_random_engine generator; std::uniform_int_distribution<int> distribution( 0, n/k ); x = distribution( generator ) * k;

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 + ")" )

**PC Mag,** GENERATING RANDOM NUMBERS While working in Lotus 1-2-3, UP and LOW is evenly divisible by INT, then generates the appropriate random-number formula. By itself, the RAND function 5 _ I. included in all spreadsheet pro~ grams Writing a formula to produce a random number between some other lower and You can use this random number generator to pick a truly random number between any two numbers. For example, to get a random number between 1 and 10, including 10, enter 1 in the first field and 10 in the second, then press "Get Random Number". Our randomizer will pick a number from 1 through 10 at random. To generate a random number between 1 and 100, do the same, but with 100 in the second field of the picker.

**The C++ Standard Library: A Tutorial and Reference,** They explain why using rand(), the standard random-value generator of C, causes problems that engines in general have. Many pseudo-random-number generator implementations give remainders that aren't very On the other hand, if the value of n is large, and the generated maximum value is not evenly divisible by The new random-number generators in SAS provide analysts with fast, high-quality, state-of-the-art random numbers. New random-number generators in SAS. Prior to SAS 9.4M5, SAS supported the Mersenne twister random-number generator (introduced in SAS 9.1) and a hardware-based RNG (supported on Intel CPUs in SAS 9.4M3).

**$RANDOM: generate random integer,** Another Look at Variables, Next Anyone who attempts to generate random numbers by deterministic means is, of course, RANDOM=$$ # Seeds the random number generator from PID #+ of script. echo " i.e. answer must be evenly divisible by this number. For example: 0.822725 # Uses the awk rand() function. Unfortunately, I need 20,000 random codes, and I keep freezing SSI Web by trying to paste that many items into a list. Is there a way to use the RandNum or SysRand functions to generate numbers divisible by 3? I know I can use them to get 5-digit numbers (by setting Min and Max to 10000 and 99999). Any help is much appreciated!

**9.6. random — Generate pseudo-random numbers,** This module implements pseudo-random number generators for various the basic function random(), which generates a random float uniformly in the semi-open This method is supplied with the MersenneTwister generator and some other random.randrange([start], stop[, step])¶: Return a randomly selected element Here are the steps to generate random numbers using RANDBETWEEN: Select the cell in which you want to get the random numbers. In the active cell, enter =RANDBETWEEN (1,100). Hold the Control key and Press Enter.