问题:  长度为 10000 的数组,   随机产生范围为1-100的整数 ,然后放入这个这个数组中,如何最效率?数组高效随机Java面试题

解决方案 »

  1.   

    import java.util.Random;public class java_4 {

    public static void main(String[] args) {

    int[] array = new int[10000];
    Random ran = new Random();

    for(int x: array)
    array[x] = ran.nextInt(101);
    }}
    这高效么……新手,求挑错……
      

  2.   

    这个是不是就可以看做求 0...9999 这样一个序列 ?private void mine() {
    int[] array = new int[10000];
    Random ran = new java.util.Random();
    for (int i = 0; i < 100; i ++) {
    for (int j = 0; j < 100; j ++) {
    array[100 * i + j] = ran.nextInt(101);
    System.out.println(100 * i + j);
    }
    }

    }
      

  3.   

    我虽然不知道怎么样才能最效率,但我知道调API在面试中是拿不到分数的。
      

  4.   

    面试官考你“怎样取随机数”的思路,虽然你没学过“伪随机数”相关的算法,但你要想方设法用你学过的知识往这方面靠拢,即使想到了Math.random(),也要告诉他“除了这个方法,别的方法我再研究研究”之类的话,千万不能扔给面试官API方法之后就完事了
    这题目估计要用到概率方面的知识,期待概率论的大神来解答。
      

  5.   

    import java.util.Arrays;
    import java.util.Random;
    public class Demo {
    public static void main(String[] args) {
    int[] a=new int[100];
    Random r=new Random();
    for(int i=0;i<a.length;i++){
    int s=r.nextInt(100)+1;
    a[i]=s;
    }
    System.out.println(Arrays.toString(a));
    }
    }
      

  6.   

    我并不觉得用API方法是什么问题,因为一个随机数方法是经过很多数学论证,它要尽量达到均匀分布(即不能某一个数或者某个数的倍数特别多),这并不是在面试这么点时间就能正确解决的问题。我倒是觉得面试官在考的是怎样重复使用已经生成的随机数。比如:
    随机生成随机序列A[100], B[100],你可以先用A[1...100]填充,再用B[1...100]填充,再用A[B[1..100]]填充,这样你填充了300个,却只生成了200次随机数,而在A[]和B[]都为随机序列的情况,A[B[n]]也是随机数(直觉,待论证)
      

  7.   

    import java.util.Random;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;public class TenThousandRandom implements Runnable {[code=java] static final int arrayLength = 1000000; // 要生产的数组规模
    static final int threadSize = 10; // 线程数量 static int result[] = new int[arrayLength]; static Random ran = new Random(); int threadNo; public TenThousandRandom(int defineNo) {
    threadNo = defineNo;
    } /**
     * @param args
     */
    public static void main(String[] args) { long beg = System.currentTimeMillis();
    myMethod();
    long end = System.currentTimeMillis(); System.out.println("Running time is: " + (end - beg) + "ms"); } @Override
    public void run() {
    for (int i = 0; i < (arrayLength / threadSize); i++) {
    result[threadNo + threadSize * i] = ran.nextInt(101);
    // System.out.println("Thread-" + threadNo +" generate " +
    // result[threadNo + 10*i]);
    } } public static void myMethod() { ExecutorService execServ = Executors.newFixedThreadPool(threadSize);
    // 启动10个线程
    for (int j = 0; j < threadSize; j++) {
    execServ.execute(new Thread(new TenThousandRandom(j)));
    } execServ.shutdown();
    if (execServ.isShutdown()) {
    System.out.println("Muti-Thread is finished");
    // for (int i = 0; i < arrayLength; i++) {
    // System.out.println(result[i]);
    // } return; }
    } public static void otherMethod() {
    for (int i = 0; i < arrayLength; i++) {
    int s = ran.nextInt(100) + 1;
    result[i] = s;
    }
    }
    }
    [/code]
      

  8.   

    import java.util.Random;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;public class TenThousandRandom implements Runnable { static final int arrayLength = 1000000; // 要生产的数组规模
    static final int threadSize = 10; // 线程数量 static int result[] = new int[arrayLength]; static Random ran = new Random(); int threadNo; public TenThousandRandom(int defineNo) {
    threadNo = defineNo;
    } /**
     * @param args
     */
    public static void main(String[] args) { long beg = System.currentTimeMillis();
    myMethod();
    long end = System.currentTimeMillis(); System.out.println("Running time is: " + (end - beg) + "ms"); } @Override
    public void run() {
    for (int i = 0; i < (arrayLength / threadSize); i++) {
    result[threadNo + threadSize * i] = ran.nextInt(101);
    // System.out.println("Thread-" + threadNo +" generate " +
    // result[threadNo + 10*i]);
    } } public static void myMethod() { ExecutorService execServ = Executors.newFixedThreadPool(threadSize);
    // 启动10个线程
    for (int j = 0; j < threadSize; j++) {
    execServ.execute(new Thread(new TenThousandRandom(j)));
    } execServ.shutdown();
    if (execServ.isShutdown()) {
    System.out.println("Muti-Thread is finished");
    // for (int i = 0; i < arrayLength; i++) {
    // System.out.println(result[i]);
    // } return; }
    } public static void otherMethod() {
    for (int i = 0; i < arrayLength; i++) {
    int s = ran.nextInt(100) + 1;
    result[i] = s;
    }
    }
    }
      

  9.   

    import java.util.Arrays;
    import java.util.Random;
    public class Suiji {
    public static void main(String[] args) {
    int[] a=new int[100];
    Random r=new Random();
    for(int i=0;i<a.length;i++){
    int s=r.nextInt(10000);
    a[i]=s;
    }
    System.out.println("您的随机产生的数:");
    System.out.println(Arrays.toString(a));
    }
    }