JAVA 怎么得到数组的随机下标?

JAVA 怎么得到数组的随机下标?,第1张

楼上的方法会产生数组越界。应该用这样的方式分别得到一维和二维的,先定义一个随即对象,并填入种子:Random

rand=new

Random();而后开始产生维数:一维:randnextInt(alength),二维:randnextInt(a[0]length),这样就能从数组中取得随机元素了。int

c=a[randnextInt(alength)][randnextInt(a[0]length)]

import javautilRandom;

public class Test16 {

public static void main(String[] args) {

Systemoutprintln(getRandomNumber(7));

}

/

得到n位长度的随机数

@param n 随机数的长度

@return 返回 n位的随机整数

/

public static int getRandomNumber(int n){

int temp = 0;

int min = (int) Mathpow(10, n-1);

int max = (int) Mathpow(10, n);

Random rand = new Random();

while(true){

temp = randnextInt(max);

if(temp >= min)

break;

}

return temp;

}

}

Java源程序:

import javautilRandom;

public class Exam07 {

public static void main(String args[]){

Random rand = new Random();

int i, n;

for(i=1; i<=100; i++){

while((n = randnextInt(10000)) < 1000);

Systemoutprint(n + " ");

if(i % 10 == 0){

Systemoutprintln();

}

}

}

}

第一次运行测试:

7963 5767 9343 9785 3269 5245 6896 8479 5400 4758

7814 9282 6559 3609 3573 5827 3125 2432 5393 9079

8752 1243 1996 8420 9579 1972 9245 3188 3519 3789

4409 4396 1629 2693 2817 4490 4778 6584 4907 6504

5001 9151 9885 5217 1872 7871 8209 1733 1779 6168

3705 4112 9903 9293 5611 6626 3617 3083 4366 9977

3106 8086 7984 5179 6520 5483 1587 8639 3324 8018

7381 9900 9283 9983 9706 9945 3195 7224 3544 6324

3139 9969 4383 8654 8408 9251 2035 7283 2760 2603

5543 6615 5365 7518 9320 5932 4264 3162 4931 6256

第二次运行测试:

6557 2822 4421 8256 2216 5200 8065 1905 3370 3090

1801 2823 8975 6393 9585 5645 2738 6385 1007 6738

1282 6930 4073 4013 7524 2947 9524 5750 1410 9549

8198 6612 9556 2258 1460 5710 1136 3604 9195 6843

7692 5729 6841 1887 4270 8864 5215 3098 9620 7261

6577 2067 8759 3476 3903 8839 2803 1291 8187 4113

4703 8564 7786 4705 1588 1523 9972 2696 2179 3448

6198 7430 4806 7730 8191 2952 6240 7756 6164 9587

9689 9954 4110 6966 9462 1240 5292 6943 2651 3985

8287 3227 9127 2814 3393 4546 5414 7002 6553 7681

我服了,我把源码贴出来吧

public class Client {

    //发送账单的数量,这个值是从数据库中获得

    private static int MAX_COUNT = 6;

     

    public static void main(String[] args) {

        //模拟发送邮件

        int i=0;

        //把模板定义出来,这个是从数据库中获得

        Mail mail = new Mail(new AdvTemplate());

        mailsetTail("XX银行版权所有");

        while(i<MAX_COUNT){              

            //以下是每封邮件不同的地方

            mailsetAppellation(getRandString(5)+" 先生(女士)");

            mailsetReceiver(getRandString(5) + "@" + getRandString(8)+"com"); 

            //然后发送邮件

            sendMail(mail);

            i++;

        }

    }   

    //发送邮件

    public static void sendMail(Mail mail){

        Systemoutprintln("标题:"+mailgetSubject() + "\t收件人:"+mailgetReceiver()+"\t发送成功!");

    }   

    //获得指定长度的随机字符串

    public static String getRandString(int maxLength){

        String source ="abcdefghijklmnopqrskuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        StringBuffer sb = new StringBuffer();

        Random rand = new Random();

        for(int i=0;i<maxLength;i++){

            sbappend(sourcecharAt(randnextInt(sourcelength())));

        }

        return sbtoString();       

    }

}

1、获取数组长度

int arr[] = {1,2,3,4,5};//定义一个数组

int len = arrlength;//获取数组长度给变量len

2、根据数组长度,使用Random随机数组的索引值

Random random = new Random();//创建随机对象

int arrIdx = randomnextInt(len-1);//随机数组索引,nextInt(len-1)表示随机整数[0,(len-1)]之间的值

3、根据随机索引获取数组值

int num = arr[arrIdx];//获取数组值

Mathrandom()返回的只是从0到1之间的小数,如果要50到100,就先放大50倍,即0到50之间,这里还是小数,如果要整数,就强制转换int,然后再加上50即为50~100

最终代码:(int)(Mathrandom()50) + 50

Random random = new Random();//默认构造方法

Random random = new Random(1000);//指定种子数字

在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要的随机数字。

相同种子数的Random对象,相同次数生成的随机数字是完全相同的。也就是说,两个种子数相同的Random对象,第一次生成的随机数字完全相同,第二次生成的随机数字也完全相同。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/12184404.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存