//返回一个范围在[low,high]之间的数,low,high均为正数,low为低值,hight为高值 //注意是[low,high]范围,返回的数包括了low和high //如果返回了-1说明调用出错public int randomNumber (int low,int high) { int temp=-1; if (low<=high) { temp=low+r.nextInt(high+1); return temp; } return temp; }调用:int x=randomNumber(1,3);System.out.println(x);楼主后面那个问题我正好在想,等我做了再发上来
其实不管你的数字是否连续比如{1,3,6,8} 你只需要去取序号的随机数 比如a[4] = {1,3,6,8} 随机数就是 a[getRandomInt(4)]/** *0 到n之间的随机数 * * @param n * int * @return int */ public static int getRandomInt(int n) { Random rand = new Random(); return rand.nextInt(n + 1); }
错了不好意思 应该是a[getRandomInt(3)]不是a[getRandomInt(4)]
怎么不行了?import java.util.*; public class Test { public static void main(String[] args) { int a[] = {1,3,6,7}; for(int i =0 ;i<100;i++){ System.out.println(a[getRandomInt(3)]); } } public static int getRandomInt(int n) { Random rand = new Random(); return rand.nextInt(n + 1); } }
Random r = new Random(); int i =r.nextInt(3)+1; System.out.println(i);
import java.util.*; import java.io.*; class RandomNumbers { Random r=new Random(); public void RandomNumbers (){ }public int randomNumber (int low,int high) { int temp=-1; if (low<=high) { temp=low+r.nextInt(high); return temp; } return temp; } } //调用public class RandomNumber { static RandomNumbers rnb=new RandomNumbers();
public static void main(String[] args) {
int y=rnb.randomNumber(1,3); for (int i=0;i<20;i++) { int x=rnb.randomNumber(1,3); System.out.println(x); }
}
}
想输出类似 1 3 6 的随即数 就让他输出到6 然后加个if判断 Random r = new Random(); int i =r.nextInt(6)+1;if(i==1||i==3||i==6){ System.out.println(i);}这样就行了~
int i = (int)(Math.ceil(r.nextInt(3)+r.nextDouble()))很繁!但可以实现
int i =r.nextInt(3)+1;
就可以了
即产生 0~n-1 所以如果要产生 123 就要在后面加1
否则不会有 3出现
//注意是[low,high]范围,返回的数包括了low和high
//如果返回了-1说明调用出错public int randomNumber (int low,int high)
{
int temp=-1;
if (low<=high)
{
temp=low+r.nextInt(high+1);
return temp;
}
return temp;
}调用:int x=randomNumber(1,3);System.out.println(x);楼主后面那个问题我正好在想,等我做了再发上来
你只需要去取序号的随机数
比如a[4] = {1,3,6,8}
随机数就是 a[getRandomInt(4)]/**
*0 到n之间的随机数
*
* @param n
* int
* @return int
*/
public static int getRandomInt(int n) {
Random rand = new Random();
return rand.nextInt(n + 1);
}
应该是a[getRandomInt(3)]不是a[getRandomInt(4)]
public class Test {
public static void main(String[] args) {
int a[] = {1,3,6,7};
for(int i =0 ;i<100;i++){
System.out.println(a[getRandomInt(3)]);
}
}
public static int getRandomInt(int n) {
Random rand = new Random();
return rand.nextInt(n + 1);
}
}
int i =r.nextInt(3)+1;
System.out.println(i);
import java.io.*;
class RandomNumbers {
Random r=new Random();
public void RandomNumbers (){
}public int randomNumber (int low,int high)
{
int temp=-1;
if (low<=high)
{
temp=low+r.nextInt(high);
return temp;
}
return temp;
}
}
//调用public class RandomNumber {
static RandomNumbers rnb=new RandomNumbers();
public static void main(String[] args) {
int y=rnb.randomNumber(1,3);
for (int i=0;i<20;i++)
{
int x=rnb.randomNumber(1,3);
System.out.println(x);
}
}
}
就让他输出到6
然后加个if判断
Random r = new Random();
int i =r.nextInt(6)+1;if(i==1||i==3||i==6){
System.out.println(i);}这样就行了~