生成均分的数字 [英] Generate numbers that divide evenly

查看:65
本文介绍了生成均分的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种简单的方法来生成两个数字,并确保一个数字可以被另一个数字整除.

I was wondering if there was an easy way to generate 2 numbers and make sure that one is evenly divisible by the other.

这是我到目前为止所拥有的:

This is what i have so far:

Random rand = new Random();
int  d = rand.nextInt(90)*2 + 1;
int dd = rand.nextInt(d)+1;

推荐答案

如果要生成两个数字a和b,其中a除以b即存在一个整数k,其中a * k = b可以随机生成a,然后随机生成k并将其乘以得到b.

If you want to generate two numbers a and b where a divides b I.e. there exists an int k where a*k =b you can randomly generate a and then randomly generate k and multiply the two to get b.

这篇关于生成均分的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆