If we want to create 2 random numbers so that the first number can be divided by the second (simple mathematical division). But so that without the rest! How to generate these 2 numbers?

That's what I tried, but I still have the remainder of the division.

var a = Math.floor(Math.random()*90 + 1); var b = Math.floor(Math.random()*10 + 1); var result = a / b; alert(result); 
  • 3
    Generate multipliers, not divisors and dividends. - Visman
  • 2
    The simplest is pseudocode: a = random (); b = random (); a = a * b; - Firepro

3 answers 3

 var result = Math.floor(Math.random()*9 + 1); var b = Math.floor(Math.random()*10 + 1); var a = result * b; alert(result); 
  • All you did was remade it into multiplication, did you read my question? I need a division without a balance ... - arthru
  • 2
    @arthru we read your question: "create 2 random numbers ..." Did you read the answer? - Igor

First generate randomly, then generate a randomly small multiplier. The second is equal to the product of the first factor. And do not need any sorting

    Check that the number a is divided by the number b without the remainder:

     if (!(a % b)){...} 

    Accordingly, you can do generation in a cycle. Or choose one number, multiply it by another and get the required numbers.

    • HM interesting. And how to do what if it can not share, then try to generate another number, and so until you find the right one? - arthru
    • @arthru "can do generation in a cycle.", wrote the same. - Vladimir Martyanov
    • I realized that I needed something like while (! (A% b)) {Can you help with the code? - arthru
    • Only while (a% b), and so you have already written almost all the code yourself. - Vladimir Martyanov
    • Well, what's inside the best time to write? Can you help with this? - arthru