如何在带有数组边界的java中生成随机数?

时间:2022-11-28 16:50:28

I want to ask how to generate a random number in java , i know it is done by random.nextint() but i want to check if the number is not what i wanted then it should be rejected and a new random number should be generated .

我想问一下如何在java中生成一个随机数,我知道它是由random.nextint()完成的,但我想检查数字是不是我想要的那么它应该被拒绝并且应该生成一个新的随机数。

I want something like this :

我想要这样的东西:

Integer[] in = {1,2,3,4,5};    
int a = new Random().nextInt(10);
for(int i=0;i<in.length ;i++)
   if(a==in[i])
      //new random number

if the number is present in the above array (in) then new random number should be generated

如果数字出现在上面的数组(in)中,那么应该生成新的随机数

4 个解决方案

#1


2  

Just put it in a do-while loop:

把它放在一个do-while循环中:

int a;
do {
    a = new Random().nextInt(10);
} while (Arrays.asList(in).contains(a));

#2


1  

I would avoid not generating a number you didn't want in the first place.

我会避免在第一时间没有生成你不想要的数字。

You can do either

你也可以

int a = random.nextInt(5);
if (a > 0) a += 5;

or use a selection

或使用选择

int[] valid = { 0, 6, 7, 8, 9 }; // 0 to 9 but not 1,2,3,4,5
int a = valid[random.nextInt(valid.length)];

#3


0  

Simply call the method again. That is, if the number generated fits the if criteria then call a = new Random().nextInt(10);

只需再次调用该方法即可。也就是说,如果生成的数字符合if标准,则调用a = new Random()。nextInt(10);

Or, if your for loop ever regenerates a random number, you could just have the if statement do nothing ex: if(xyz){}; which of course would be pointless, and I think the original solution is probably what you seek.

或者,如果你的for循环曾经重新生成一个随机数,你可以让if语句不做任何事情:if(xyz){};这当然没有意义,我认为最初的解决方案可能就是你所追求的。

#4


0  

To avoid any loops and retrying, try this:

要避免任何循环并重试,请尝试以下操作:

    int [] in = {1,2,3,4,5};
    // generate integers from 0 up to the size of your array of allowed numbers:
    int index = new Random().nextInt(in.length); 
    int a = in[index]; // use the random integer as index for your array of allowed numbers

#1


2  

Just put it in a do-while loop:

把它放在一个do-while循环中:

int a;
do {
    a = new Random().nextInt(10);
} while (Arrays.asList(in).contains(a));

#2


1  

I would avoid not generating a number you didn't want in the first place.

我会避免在第一时间没有生成你不想要的数字。

You can do either

你也可以

int a = random.nextInt(5);
if (a > 0) a += 5;

or use a selection

或使用选择

int[] valid = { 0, 6, 7, 8, 9 }; // 0 to 9 but not 1,2,3,4,5
int a = valid[random.nextInt(valid.length)];

#3


0  

Simply call the method again. That is, if the number generated fits the if criteria then call a = new Random().nextInt(10);

只需再次调用该方法即可。也就是说,如果生成的数字符合if标准,则调用a = new Random()。nextInt(10);

Or, if your for loop ever regenerates a random number, you could just have the if statement do nothing ex: if(xyz){}; which of course would be pointless, and I think the original solution is probably what you seek.

或者,如果你的for循环曾经重新生成一个随机数,你可以让if语句不做任何事情:if(xyz){};这当然没有意义,我认为最初的解决方案可能就是你所追求的。

#4


0  

To avoid any loops and retrying, try this:

要避免任何循环并重试,请尝试以下操作:

    int [] in = {1,2,3,4,5};
    // generate integers from 0 up to the size of your array of allowed numbers:
    int index = new Random().nextInt(in.length); 
    int a = in[index]; // use the random integer as index for your array of allowed numbers