How to get 2 random numbers divisible by each other?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to get 2 random numbers divisible by each other?



I'm generating 2 random numbers:



1) The first number must be random from 1 to 30



2) The second number must be random from 1 to 10



I'm simply trying to have the first number divisible by the second number or vice-versa, and finally, alert the result. My question is how to get the result of the division of 2 random numbers? Can anyone point me in the right direction? Thanks a lot in advance!.



Note: the first number must be divisible by the second number.



Here's my code:


var arr = ;
for (var i = 0; i < 2; i++)
do
var firstRandomNumber = Math.floor(Math.random()*30) + 1;
var secondRandomNumber = Math.floor(Math.random()*10) + 1;
if(firstRandomNumber % secondRandomNumber === 0)
correctResult = result;
arr.push(correctResult);

while ((firstRandomNumber % secondRandomNumber === 0));

console.log(arr);





You have both: first number divisible by the second number or vice-versa and first number must be divisible by the second number which is not especially clear.
– Mark Meyer
Aug 12 at 5:28


first number divisible by the second number or vice-versa


first number must be divisible by the second number




5 Answers
5



Few things:


while


firstRandomNumber % secondRandomNumber === 0


result


if(firstRandomNumber % secondRandomNumber === 0){


arr.push()




var arr = ;
for (var i = 0; i < 2; i++)
do
var firstRandomNumber = Math.floor(Math.random()*30) + 1;
var secondRandomNumber = Math.floor(Math.random()*10) + 1;
while ((firstRandomNumber % secondRandomNumber !== 0));

console.log('first', firstRandomNumber, 'second', secondRandomNumber);
arr.push(firstRandomNumber / secondRandomNumber);

console.log(arr);





can you update your answer to only add ONE element to the array (the result of the division) and also console out the 2 numbers that are being divided with each other? Thanks a lot in advance!
– progx
Aug 12 at 5:12





@progx I added the console.log, and the div result is already adding to the array. Just remove the for loop if you only want one number.
– FrankerZ
Aug 12 at 5:16


console.log





Thanks a lot for your help bro!!
– progx
Aug 12 at 5:20



I would suggest a more functional approach: create a function that creates two random numbers, and returns them if one is divisible by the other. Then, just call that function until you get a truthy result:




function tryGetDivisible()
var firstRandomNumber = Math.floor(Math.random() * 30) + 1;
var secondRandomNumber = Math.floor(Math.random() * 10) + 1;
if (firstRandomNumber % secondRandomNumber === 0)
console.log(firstRandomNumber, secondRandomNumber);
return firstRandomNumber / secondRandomNumber;



let result;
while (!result) result = tryGetDivisible();
const arr = [result];
console.log(arr);





Tossed you an upvote. This is a solid answer as well, and the loop is a bit cleaner.
– FrankerZ
Aug 12 at 5:24



A much simpler approach is to get the first random number, and then try getting the second random number until they are divisible. So here will be the code:


var firstRandomNumber = Math.floor(Math.random()*30) + 1;
while (firstRandomNumber % secondRandomNumber !== 0)
var secondRandomNumber = Math.floor(Math.random()*10) + 1;

console.log(firstRandomNumber + "," + secondRandomNumber);





your answer does not work if the first random number is 29, because then the second number will be to be only 1 or 29.
– progx
Aug 12 at 5:15





Still 29 is divisible by both 1 and 29. The same way 4 is divisible by 2. Unless this is an extra condition you want to have in your code, it meets your need.
– Hessam Shekhasnay
Aug 12 at 5:44



Since the first must be divisible by the second, my approach would be:


Math.floor(30/firstNumber)



This way, there's no need to do a generate-and-test loop, which could go on an unbounded number of times before a successful pair is generated.





Wouldn't this make the first number dependent on the second number? It wouldn't be truly random: For example if the second number was prime, then 1 would always be picked for the first number.
– FrankerZ
Aug 12 at 5:21






@FrankerZ - It's already dependent on the second number, since there's a requirement to reject all first numbers that are not multiples of the second number. The generate-and-test loops simply disguise the dependence.
– Ted Hopp
Aug 12 at 5:23





The randomness would not be as good as it could be. For example if the first number is 8, this method only allows for 16 and 24 as the second number when in fact, 2, 1, and 4 should be possibilities.
– Mark Meyer
Aug 12 at 5:24





@MarkMeyer - Did I misread the question? OP specifically said "the first number must be divisible by the second number". Also, note that I'm generating the second number (the one constrained to be between 1 and 10) first.
– Ted Hopp
Aug 12 at 5:26






@MarkMeyer - Good point. If it's the former, then your answer may be the best, although I'd have to think about possible bias.
– Ted Hopp
Aug 12 at 5:32




If you want to avoid the while loop, you can pick the first number, then assemble the possible second numbers in an array. Then randomly pick one of these:


while




let first = Math.floor(Math.random() * 10) + 1

// all possible divisible numbers
let factors = Array.from(length: 30, (_, i) => i + 1)
.filter(i => first % i === 0 || i % first === 0)

//pick one
let second = factors[Math.floor(Math.random() * factors.length)]

console.log(first, second)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard