使用随机算法产生一个数,要求把1-1000W之间这些数全部生成

来源:互联网 发布:知乎 农村金融数据 编辑:程序博客网 时间:2024/06/05 04:38

无聊写个玩下

private static void createRandom() {

        Random random = new Random();
        long start = System.currentTimeMillis();

        int value = 1000000;

        ArrayList<Integer> list = new ArrayList<Integer>(value);
        for (int j = 1; j <= value; ++j) {
            list.add(j);
        }

        int index = 0;
        int count = 0;
        int tmp = 0;
        while (value > 0) {
            index = random.nextInt(value);
            // System.out.println(list.get(index));
            tmp = list.get(count + index);
            list.set(count + index, list.get(count));
            list.set(count, tmp);
            ++count;
            --value;
        }

        long end = System.currentTimeMillis();

        //----验证是否正确
        Collections.sort(list);
        int i = 0, size = list.size();
        for (; i < size; ++i) {
            if (list.get(i) != (i + 1))
                System.out.println(i + "error" + list.get(i));
        }
        //----验证是否正确


        System.out.println("creat3------");
        System.out.println("执行耗时 : " + (end - start) / 1000f + " 秒 ");
        System.out.println("集合大小为" + list.size());
    }


private static void createRandom4() {

        Random random = new Random();
        long start = System.currentTimeMillis();

        int value = 10000000;
        
        int[] list = new int[value];

        for (int j = 0; j < value; ++j) {
            list[j] = j+1;
        }

        int index = 0;
        int count = 0;
        int tmp = 0;
        while (value > 0) {
            index = random.nextInt(value);
//             System.out.println(list[count + index]);
            tmp = list[count + index];
            list[count + index] = list[count];
            list[count] = tmp;
            ++count;
            --value;
        }

        long end = System.currentTimeMillis();

        //----验证是否正确
//        Collections.sort(list);
//        int i = 0, size = list.length;
//        for (; i < size; ++i) {
//            if (list[i] != (i + 1))
//                System.out.println(i + "error" + list[i]);
//        }
        //----验证是否正确

        System.out.println("creat4------");
        System.out.println("执行耗时 : " + (end - start) / 1000f + " 秒 ");
        System.out.println("完了,集合大小为" + list.length);
    }

实际测试结果:
creat3------
执行耗时 : 5.169 秒
完了,集合大小为10000000
creat4------
执行耗时 : 0.729 秒
完了,集合大小为10000000

0 0
原创粉丝点击