算法同上题

package Hard;

import CtCILibrary.AssortedMethods;

/**
* Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen. 译文: 写一个函数,随机地从大小为n的数组中选取m个整数。要求每个元素被选中的概率相等。
*
*/
public class S18_3 { /* Random number between lower and higher, inclusive */
public static int rand(int lower, int higher) {
return lower + (int)(Math.random() * (higher - lower + 1));
} public static void swap(int[] a, int n, int m){
int tmp = a[n];
a[n] = a[m];
a[m] = tmp;
} /* pick M elements from original array, using only elements 0 through i (inclusive).*/
public static int[] pickMRecursively(int[] original, int m, int i) {
if (i + 1 < m) { // Not enough elements
return null;
} else if (i + 1 == m) { // Base case -- copy first m elements into array
int[] set = new int[m];
for (int k = 0; k < m; k++) {
set[k] = original[k];
}
return set;
} else {
int[] set = pickMRecursively(original, m, i - 1);
int k = rand(0, i);
if (k < m) {
set[k] = original[i];
}
return set;
}
} /* pick M elements from original array. Clone original array so that
* we don’t destroy the input. */
public static int[] pickMRandomly(int[] original, int m) {
for (int i = 0; i < m; i++) {
int k = rand(i, original.length-1); // 产生i到n-1间的随机数
swap(original, i, k);
}
int[] subset = new int[m];
for(int i=0; i<m; i++){
subset[i] = original[i];
}
return subset;
} public static void main(String[] args) {
int[] cards = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println(AssortedMethods.arrayToString(cards));
int[] set = pickMRandomly(cards, 4);
System.out.println(AssortedMethods.arrayToString(set));
}
}

Hard 随机选择subset @CareerCup的更多相关文章

  1. python3 selenium 随机选择同一类型下的某一个元素

    使用场景: 如上图所示,有时候,我们测试的时候,不会每个方向都选择一遍,也不能每次都选择一个方向,这个时候就需要每次运行用例的时候,随机选择一个方向来测试 使用方法: random.randint() ...

  2. python random从集合中随机选择元素

    1.使用python random模块的choice方法随机选择某个元素 from random import choice foo = ['a', 'b', 'c', 'd', 'e'] print ...

  3. ISLR系列:(4.1)模型选择 Subset Selection

    Linear Model Selection and Regularization 此博文是 An Introduction to Statistical Learning with Applicat ...

  4. Java-Selenium,获取下拉框中的每个选项的值,并随机选择某个选项

    今天逛51testing,看见有人问这个问题.现在以Select标签为例. 1.首先看页面中的下拉框,如图: 2.F12查看页面源代码,如下 <select class="form-c ...

  5. random os 序列化 模块模块 随机选择

    # 1 random 模块 随机选择# import random#随机取小数# ret = random.random() #空是0到1之间的小数字# print(ret)# # 0.0799728 ...

  6. 使用Numpy验证Google GRE的随机选择算法

    最近在读<SRE Google运维解密>第20章提到数据中心内部服务器的负载均衡方法,文章对比了几种负载均衡的算法,其中随机选择算法,非常适合用 Numpy 模拟并且用 Matplotli ...

  7. php array_rand()函数从数组中随机选择一个或多个元素

    php使用array_rand()函数从数组中随机选择一个或多个元素的方法. 使用array_rand() 函数从数组中随机选出一个或多个元素,并返回.  array_rand(array,numbe ...

  8. python random 随机选择操作

    # -*- coding:utf-8 -*- import random arr = ['A','B','C','D','E','F'] #生成(0.0, 1.0)的随机数 print random. ...

  9. python: 随机选择

    想从一个序列中随机抽取若干元素,或者想生成几个随机数. random 模块有大量的函数用来产生随机数和随机选择元素.比如,要想从一个序列中随机的抽取一个元素,可以使用random.choice() : ...

随机推荐

  1. JavaScript Array(数组) 对象

    更多实例 合并两个数组 - concat() 合并三个数组 - concat() 用数组的元素组成字符串 - join() 删除数组的最后一个元素 - pop() 数组的末尾添加新的元素 - push ...

  2. SpringMVC4+thymeleaf3的一个简单实例(篇四:form表单数据验证)

    关于表单数据验证有很多中方法,这里我仅介绍JSR303注解验证.JSR303仅仅是一个规范,这里我们要用到它的一个实现:hibernate-validator. 注意在spring的配置文件sprin ...

  3. php文件加锁 lock_sh ,lock_ex

    文件锁有两种:共享锁和排他锁,也就是读锁(LOCK_SH)和写锁(LOCK_EX) 文件的锁一般这么使用: $fp = fopen("filename", "a" ...

  4. JQUERY、AJAX双击DIV,直接修改DIV内的内容

    最近在做后台功能开发的时候,用到对排序字段的修改,感觉只为了修改一个排序值,而要重新进入编辑页比较麻烦,于是自己动手写…… 最近在做后台功能开发的时候,用到对排序字段的修改,感觉只为了修改一个排序值, ...

  5. ExtJs中动态加载机制研究(转)

    觉得写的太好了,怕弄丢了,转一下:http://extjs.org.cn/node/659 昨天我们team对于extjs的动态加载机制做了些深入研究,这里先share下controller加载的结果 ...

  6. 脚本学习python和linux-shell和jQuery(javascript)

    使用脚本可以方便管理,使用计算机. 打算学脚本来更好地用计算机系统,特别是Linux. 学python因为它开源,而且是C家族的语言,本来也是课程需要,再加上它确实很好,所以非常主打,之前看过perl ...

  7. ios开发之IBOutlet和IBAction的区别

    IBOutlet 输出口是使用关键字IBOutlet声明的实例变量.控制器头文件中的输出口声明应如下所示: @property (nonatomic, retain) IBOutlet UIButto ...

  8. Entity Framework 级联删除

    为一对主从表增加级联删除功能 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.E ...

  9. excel poi 文件导出,支持多sheet、多列自动合并。

    参考博客: http://www.oschina.net/code/snippet_565430_15074 增加了多sheet,多列的自动合并. 修改了部分过时方法和导出逻辑. 优化了标题,导出信息 ...

  10. prototype/constructor/__proto__之prototype简单应用

    一.简单使用构造原型加prototype造简单的轮子. 1.想jQ那样获取HTML元素,先看JS代码 function Cmf() { //创建构造函数 this.arry = [] } Cmf.pr ...