Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

javascript - 如何在javascript中生成两个随机数,例如第一个数字始终大于第二个数字?(How to generate Two Random Number in javascript such as the 1st number is always greater than the second?)

How to generate Two Random Number in javascript such as the 1st number is always greater than the second and also in another code please share how to generate two random number such as the first number is always divisible by the second?(如何在javascript中生成两个随机数,例如第一个数字总是大于第二个,并且在另一个代码中,请分享如何生成两个随机数,例如第一个数字总是被第二个整除?)

I am trying(我在尝试) var x = (Math.floor(Math.random() * 100) + 1); var y = ((Math.floor(Math.random() * 100) + 1) <= x); console.log(x, y); but I am only getting the value of x and instead geting the value of y I am getting true or false.(但是我只得到x的值,而不是得到y的值,我得到的是对还是错。)   ask by Tamanna Hasan Mouli translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could get a number and then the parts of it and select from the parts a value.(您可以先获取一个数字,然后获取其一部分,然后从部分中选择一个值。)

function getRandomN(max) { return Math.floor(Math.random() * max) + 1; } function getRandom(array) { return array[Math.floor(Math.random() * array.length)]; } function getParts(n) { var parts = [], i; for (i = 1; i < Math.floor(n / 2); i++) { if (n % i === 0) parts.push(i); } console.log(...parts); return parts; } var value1 = getRandomN(100), value2 = getRandom(getParts(value1)); console.log(value1, value2);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...