一个java面试题,求帮助

Eviltomorrow 2013-04-27 12:08:42
问题: 长度为 10000 的数组, 随机产生范围为1-100的整数 ,然后放入这个这个数组中,如何最效率?
...全文
502 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
linhu007 2013-05-01
  • 打赏
  • 举报
回复
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)); } }
火影之贺 2013-04-30
  • 打赏
  • 举报
回复
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;
		}
	}
}
火影之贺 2013-04-30
  • 打赏
  • 举报
回复
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]
lcf 2013-04-30
  • 打赏
  • 举报
回复
引用 4 楼 SmallYamateh 的回复:
面试官考你“怎样取随机数”的思路,虽然你没学过“伪随机数”相关的算法,但你要想方设法用你学过的知识往这方面靠拢,即使想到了Math.random(),也要告诉他“除了这个方法,别的方法我再研究研究”之类的话,千万不能扔给面试官API方法之后就完事了; 这题目估计要用到概率方面的知识,期待概率论的大神来解答。
我并不觉得用API方法是什么问题,因为一个随机数方法是经过很多数学论证,它要尽量达到均匀分布(即不能某一个数或者某个数的倍数特别多),这并不是在面试这么点时间就能正确解决的问题。 我倒是觉得面试官在考的是怎样重复使用已经生成的随机数。 比如: 随机生成随机序列A[100], B[100],你可以先用A[1...100]填充,再用B[1...100]填充,再用A[B[1..100]]填充,这样你填充了300个,却只生成了200次随机数,而在A[]和B[]都为随机序列的情况,A[B[n]]也是随机数(直觉,待论证)
menglihudiefei 2013-04-30
  • 打赏
  • 举报
回复
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)); } }
gemwuu 2013-04-27
  • 打赏
  • 举报
回复
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);
	}

}
这高效么……新手,求挑错……
kosora曹 2013-04-27
  • 打赏
  • 举报
回复
引用 5 楼 groovy2007 的回复:
难道要自己实现随机数?线性同余生成器
估计得自己实现,给个思路?
groovy2007 2013-04-27
  • 打赏
  • 举报
回复
难道要自己实现随机数?线性同余生成器
kosora曹 2013-04-27
  • 打赏
  • 举报
回复
面试官考你“怎样取随机数”的思路,虽然你没学过“伪随机数”相关的算法,但你要想方设法用你学过的知识往这方面靠拢,即使想到了Math.random(),也要告诉他“除了这个方法,别的方法我再研究研究”之类的话,千万不能扔给面试官API方法之后就完事了; 这题目估计要用到概率方面的知识,期待概率论的大神来解答。
kosora曹 2013-04-27
  • 打赏
  • 举报
回复
我虽然不知道怎么样才能最效率,但我知道调API在面试中是拿不到分数的。
zhuyang7654321 2013-04-27
  • 打赏
  • 举报
回复
这个是不是就可以看做求 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);
			}
		}
		
	}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧