输入:整数m,n
输出:成0~n-1内的m个不重复的随机整数,要求按序输出,并且保证每个子集被选中的可能性相等。
伪代码:
select = m
remaining = n
for i = (0]
if(bigrand()%remaining) < select
print i
select --
remainning --
书中p120页中的主要思想是利用《计算机程序设计艺术第2卷 半数值算法》中给出了伪代码生成数值的等概率性。下面给出java实现代码
package org.mino.sort; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Random; /** * 随机获取有序数 * @author DingJie */ public class RandomOrderNum { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { long beginTime = System.currentTimeMillis(); FileWriter fw = null; BufferedWriter bw = null; try { fw = new FileWriter("D:/randomOrderNum.txt"); bw = new BufferedWriter(fw); RandomOrderNum.getOrderRandomNum(999, 1000000, bw); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if(bw != null) { bw.flush(); bw.close(); } /*if(fw != null) { fw.flush(); fw.close(); }*/ } long endTime = System.currentTimeMillis(); System.out.println("总共用时(秒):" + (endTime - beginTime) /1000); } /** * 获取随机数 * @param m * @param n * @param ow */ public static void getOrderRandomNum(int m, int n, BufferedWriter bw) { Random rand = new Random(1000000); for(int i=0; i < n ; i++) { int randInt = Math.abs(rand.nextInt()); if(randInt % (n-i) < m) { // System.out.println(" random: "+randInt % (n-i)); try { bw.write(String.format("%d", i)); System.out.println(i); bw.newLine(); bw.flush(); } catch (IOException e) { e.printStackTrace(); } m --; } } } }
还有一种方法是通过m维数组,指定初始值为数组下标值的大小,然后对每一下标的值与等概率生成的下标进行交换。
package org.mino.perl; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Random; /** * 随机产生1-n的m个数 * @author DingJie */ public class RandomGenerate { public static void main(String[] args) { if (args.length == 0) { System.out.println("请输入两个整形参数 例如 9999 1000000 "); return; } int m = Integer.parseInt(args[0]); int n = Integer.parseInt(args[1]); long l1 = System.currentTimeMillis(); try { PrintWriter pw = new PrintWriter("D:/randomOrderNum.txt"); random(m, n, pw);//将随机数输出 pw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } long l2 = System.currentTimeMillis(); System.out.println("time:" + (l2 - l1)); } static int randInt(int i, int j, Random rand) { if (i < j) return i + rand.nextInt(j - i + 1); return i; } /** * 随机数输出到文件 * @param m * @param n * @param pw */ static void random(int m, int n, PrintWriter pw) { int[] array = new int[n];//最大的数组 Random rand = new Random(System.currentTimeMillis()); System.out.println(System.currentTimeMillis()); for (int i = 0; i < n; i++) array[i] = i + 1; //赋初值 for (int i = 0; i < m; i++) { int j = randInt(i, n - 1, rand);//返回从i 到n-1之间的任意随机数 int temp = array[j]; array[j] = array[i]; array[i] = temp; } for (int i = 0; i < m; i++) { pw.println(array[i]); } pw.flush(); } }
相关推荐
编程珠玑书后源代码编程珠玑书后源代码编程珠玑书后源代码编程珠玑书后源代码编程珠玑书后源代码编程珠玑书后源代码
我觉得不错,和大家分享! 编程珠玑 编程珠玑 编程珠玑
编程珠玑编程珠玑
编程珠玑和编程珠玑续两本,上传赚点分,填充填充填充
《编程珠玑(续)》是计算机科学方面的经典名著《编程珠玑》的姊妹篇,讲述了对于程序员有共性的知识。书中涵盖了程序员操纵程序的技术、程序员取舍的技巧、输入和输出设计以及算法示例,这些内容组成一个有机的整体,...
本资源只是“编程珠玑之第二章questionC: 求变位词问题”的简单的测试数据。
编程珠玑续、编程珠玑续本、编程珠玑续本、编程珠玑续本
为此,本书给出了一些精心设计的有趣而且颇具指导意义的程序,这些程序能够为那些复杂的编程问题提供清晰而且完备的解决思路,书中还充满了对实用程序设计技巧及基本设计原则的清晰而睿智的描述。
《编程珠玑》第一版是我早期职业生涯中阅读过的对我影响较大的书籍之一,在书中首次接触到的很多观点都让我长期受益。作者在这一版本中做了重要更新。新增加的很多例子让我耳目一新。 ——Steve McConnell,《代码...
编程珠玑II(编程珠玑·续) 扫描版6.56M pdf格式
编程珠玑是一本提升coding能力不可多得的好书,看书时,可以结合这个笔记,突出重点。
编程珠玑+续
书的内容围绕程序设计人员面对的一系列实际问题展开。作者Jon Bentley 以其独有的洞察力和创造力,引导读者理解这些问题并学会解决方法,而这些正是程序员实际编程...《编程珠玑(第2版)》是计算机科学方面的经典名著。
这本书是《编程珠玑》高清pdf,如有侵权请告知。
编程珠玑(第二版)答案
《编程珠玑》读书笔记
编程珠玑.pdf 面试必备,算法必备,各种算法的精彩解析
编程珠玑第二版及源代码实现(C/C++) 如果让程序员们列举他们喜欢的书籍,Jon Bentley的《编程珠玑》一定可以归于经典之列。如同精美的珍珠出自饱受沙砾折磨的牡蛎,程序员们的精彩设计也来源于曾经折磨他们的实际...
编程珠玑高清pdf版.pdf