java生成隨機數(shù)
導(dǎo)入包
import java.util.Random;
生成一個隨機數(shù)
Random random = new Random();
int num =random.nextInt();
生成一個 [ 0 , b o u n d ) \color{red}{[0,bound)} [0,bound)的隨機數(shù)
??bound 必須是正數(shù)。
以下代碼生成的是0到30的隨機數(shù)。
Random random = new Random();
int num = random.nextInt(31);
生成一個 20 \color{red}{20} 20到 90 \color{red}{90} 90的隨機數(shù)
Random random =new Random();
int num = random.nextInt(71)+20;
總結(jié)
生成區(qū)間的隨機數(shù):[最小值,最大值]
int num =random.nextInt(最大值-最小值+1)+最小值;
現(xiàn)在嘗試生成 ? 10 \color{red}{-10} ?10到 10 \color{red}{10} 10之間的隨機數(shù)
int num = random.nextInt(21)-10;
文章來源:http://www.zghlxwxcb.cn/news/detail-466385.html
學(xué)的不是技術(shù),更是夢想?。。?span toymoban-style="hidden">文章來源地址http://www.zghlxwxcb.cn/news/detail-466385.html
到了這里,關(guān)于java生成隨機數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!