我可以使用(max-min(+min公式创建1-10的随机数。
这是一个用来制造1-10之间的随机数的代码。
@Override
public void onClick(View view) {
Random random = new Random ( );
int val = random.nextInt (11-1)+1;
textview_random_number.setText ( Integer.toString ( val ) );
这是针对10-99 的
@Override
public void onClick(View view) {
Random random_two_digits = new Random ( );
int val_two_digits = random_two_digits.nextInt (100-10)+10;
textView_two_digits.setText ( Integer.toString ( val_two_digits ) );
但我想生成具有特定数或唯一数的随机数。
以下是具体数字。
128137146236245290380470489560678579
129138147156237246345390480570679589
120139148157238247256346490580670689
130149158167239248257347356590680789
140159168230249258267348357456690780
123150169178240259268349358457367790
124160179250269278340359368458467890
125134170189260279350369378459567468
126135180234270289360379450469478568
127136145190235280370479460569389578
100119155227335344399588669
200110228255336499660688778
300166229337355445599779788
400112220266338446455699770
500113122177339366447799889
600114277330448466556880899
700115133188223377
您可以将所有这些数字存储在一个数组中,并使用您已经拥有的随机函数在该数组中选择一个数字。这应该有效:
int[] array = new int[]{128, 137, 146, 236, 245, 290, 380, 470, 489, 560, 678, 579};
final int min = 0;
final int max = array.length - 1;
final int random = new Random().nextInt((max - min) + 1) + min;
int randomIntInArray = array[random];
较短版本:
int[] array = new int[]{128, 137, 146, 236, 245, 290, 380, 470, 489, 560, 678, 579};
final int random = new Random().nextInt(array.length);
int randomIntInArray = array[random];
请注意,Random
不应用于任何关键内容,因为它在加密方面不安全。对于更不可预测的结果,可以使用SecureRandom
。请参阅文档。